Re: Strange behaviour of static declared content.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Stut wrote:
> Colin Guthrie wrote:
>> Stut wrote:
>>> Stut wrote:
>>>> In that case you need a new foo. That's the only way you're going to
>>>> reset the internal static if the API doesn't give you a way to do it.
>>>>
>>>> $f=new foo();
>>>> $f->bar();
>>>> $f->bar();
>>>> $g=new foo();
>>>> $g->bar();
>>> Actually, scratch that, won't work. Not even unsetting $f before
>>> creating the new object works. This kinda sucks since it means PHP does
>>> not support static function-scoped vars.
>>
>> Yeah I tried that same thing too and then wondered if I had
>> misinterpreted how function-scoped statics worked.
>>
>> I've often used a method like:
>>
>> function Init()
>> {
>>  static $bln_inited = false;
>>  if (!$bln_inited)
>>  {
>>   // Do stuff
>>   $bln_inited = true;
>>  }
>> }
>>
>>
>> I had always assumed that the static definition here was
>> function-scoped... I guess I should have tested more but still it caught
>> me off guard this morning when I played with it.
>>
>> Correct me if I'm wrong but does C++ not do it as both of us initially
>> thought? e.g. static is function scoped rather than globally scoped when
>> used within a class method?
> 
> Yes it does, which is why I assumed PHP would do it like that too. I
> knew I should have tried it before sending the reply.
> 
> I've tried various ways of accessing that variable via a derived class
> but it doesn't seem to be possible. Again I'm just assuming, but my
> theory is that the line...
> 
>     static $foobar = false;
> 
> ...actually is static to the function and therefore cannot be accessed
> from outside it in any way, shape or form.
> 
> So using function statics as you have above will work fine. And they
> also work fine when used in a method of a class. This behaviour is
> correct because if a function-scoped static is to work correctly there
> should be absolutely no way to get at it from outside the function.

I completely agree that there should be no way to access it outside the
function/method it is defined within. The problem is that in PHP the
static keyword seems to be globally unique but function/class scoped.
e.g. that if it is used in a class method, regardless of how many
instances of that method you create with "new" there is only ever one
"instance" of the variable.

So the case where it wont work fine is when you have the above function
as a method of a class which can have multiple instances, all of which
need to be Init()'ed independently.

In such cases, I guess you'd have to use a private member variable
instead which just isn't quite a neat and tidy.

If you only ever have one instance of the class then all is well but
still could have unexpected side effects if you later extend the system
to have more than one instance. I guess if you use a singltron method
with a private constructor then it's safe enough but then you can just
do your initialisation in the singletron method in that case so there's
little point!

Hey ho.

/me scuttles of to grep code for "static" :s

Col

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux