Re: static vs global variable [message #172714 is a reply to message #172712] |
Sun, 27 February 2011 10:04 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
Bernd Schulz wrote:
> Am 26.02.2011 01:40, schrieb tobycraftse(at)yahoo(dot)com:
>>
>> 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
>
> Why not using a singleton class?
>
> Short example:
> class MYVAR
> {
> function getInstance()
> {
> static myobj = false;
> if (myobj === false)
> myobj = new MYVAR;
> return myobj;
> }
> }
>
> Using it:
> $x = MYVAR::getInstance();
> $x->global = 7;
>
> etc...
>
>
>
And what differentiates that from:
global $my_dangerous_global_x;
$my_dangerous_global_x=7;
??
Apart from the fact that the latter is a lot less lines overall. And
probably executes faster.
You HAVE declared a global variable in philosophical terms. You have
just made it lexically complicated (and in machine cycles, longer) to
access it.
Imagine trying to run the batsman out, but being REQUIRED to only throw
the ball to the wicket keeper, as he is the only man allowed to throw it
at the stumps..
All you do is shift your global object (the stumps) from being a simple
target, to the wicket keeper being the global object and the only way to
access the stumps.
I can see the point if you are playing cricket with 17 balls at once,
and you cant have two balls hitting it simultaneously, but just to play
the simple game? Pshaw!. Its madness!
|
|
|