FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » static vs global variable
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: static vs global variable [message #172700 is a reply to message #172698] Sat, 26 February 2011 15:02 Go to previous messageGo to previous message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma:
Senior Member
On 2/26/2011 9:24 AM, The Natural Philosopher wrote:
> sheldonlg wrote:
>> On 2/25/2011 7:40 PM, tobycraftse(at)yahoo(dot)com wrote:
>>>
>>> if I have a couple of variable want to included by many php file
>>>
>>> should i use global or static class variable?
>>>
>>> global is kinda trouble as i need to delcare global every php file i
>>> want to use it.
>>>
>>> static variable do not need to declare up front
>>
>> The only globals I would EVER use are the super-globals such as
>> $_SESSION.
>>
>
> That's maybe possible if you are writing OO code, but it is often
> simpler when having functions that modify several different variables,
> to have then as globals, in order not to either pass pointers to the
> functions, or try and work out a way to return more than one variable
> from a function.
>
>
>
>> So far the only real use I have found for a static class variable is
>> in setting it once when the class instance is first created. Then,
>> other invocations would be via a ClassName::getInstance() to return
>> the already created instance of the class by testing for that variable
>> not being NULL.
>>
>> Any other variable that is needed that from that class would be
>> obtained from a mutator method such as getThisVariable() which would
>> return the class variable $thisVariable.
>>
>> So, there is really only one static class variable and the rest are
>> ordinary class variables with methods provided for their access from
>> the outside once the instance is retrieved.
>>
>
> I don't do OOP, but it seems to me that a static class variable accessed

Obviously you don't.

> by a class method IS in all but name a global variable.

Not at all. Consider, for example, having a configuration file written,
say, in XML. Then you could have a Configuration.class.php file that
reads that configuration file and stores the parameters in class
variables. None of those are static, but are unique to the particular
instance of Configuration.class.php.

Now many places in the code you may need one or more of those values.
That is where it becomes useful to grab the instance already created,
which has those values already, than to have to reread the configuration
file. To do that there is a static variable in the class which was set
at the time of the first instantiation. A call to a method

$cfg = Configuration::getInstance();

returns that instance and

$cfg->getThePropertyYouWant();

returns that property.

The problem with global variables is that you don't know if somewhere
else in the code you have inadvertently changed it. Here the data are
all contained within an instance of the class. The only way anything
inside is changes is by specific action of mutator set methods. Access
is controlled by the mutator get methods. All that is done in _this_
case to retrieve and already created instance of the class, rather than
recreating it each time -- and that is done by a specific method as
well, the getInstance().

>
> The only difference being that you can somewhat control access via the
> method..

The only difference between breathing and not breathing is being alive
or dead.

>
> I wont be too definite on that point, because if I were writing
> something so complex that OOP was needed to keep track of it, for sure I
> wouldn't be writing it in PHP :-)

Your loss. PHP5 has inheritance, abstact classes, implements, and all
those other nice OO features.

>
> I wont get liked for saying it, but I regard PHP as a simple replacement
> for script or BASIC as a noddy way for inexpert programmers to rapidly
> concoct web pages with a bit more functionality. If you want to write
> large complex projects Real Men Use C++ etc etc ;-)

What do you have in C++ that you don't have in PHP other than -- UGH! --
multiple inheritance? You would be floored to see some of the stuff I
have written and seen in PHP. Also, I think Java is much better than
C++ and PHP5 is very much like Java in many ways.

>
> In short, the OOP aspects of PHP are pure pretension...

The more you post like this, the more I will have to side with Jerry.

>
> (I'll get my coat)

Good idea.

--
Shelly
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: ODBC Connection
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 08:44:08 GMT 2024

Total time taken to generate the page: 0.04636 seconds