Re: Static variable in a class method

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

 



On Thu, Feb 14, 2008 at 9:50 AM, Nathan Nobbe <quickshiftin@xxxxxxxxx> wrote:
> On Thu, Feb 14, 2008 at 6:37 AM, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote:
>
>  > Nathan Nobbe schreef:
>
> > > what you are using is potentially not what you think it is.  you are
>  > using
>  > > a 'static variable' which is not a static class member.
>  >
>  > actually it pretty much *is* the same - the static class member will
>  > exhibit the
>  > same behaviour, only the scope is different.
>  >
>  > > you can find the
>  > > doc on static variables here,
>  > > http://www.php.net/manual/en/language.variables.scope.php
>  > > im not sure if their behavior is well defined when they are used in
>  > classes,
>  > > or objects.
>  >
>  > behaviour is indentical to usage inside standalone functions.
>
>
>  thats a gamble since there is no description of how the static keyword
>  behaves inside class member functions.  i for one will stick to static class
>  variables and instance variables, and avoid this static variable feature
>  altogether.
>
>  -nathan
>

Just FYI the static keyword was quite popular in PHP4 for the
singleton pattern.  You could do something like:

function getInstance() {
    static $instance;
    if (empty($instance)) {
        $instance =& new Instance;
    }
    return $instance;
}

I've used it across multiple classes without any real conflicts so it
was fine.  Of course I wouldn't do such a thing now that I am working
in 5, but I just thought I'd throw that out there.

-- 
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