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 12:10 PM, Eric Butera <eric.butera@xxxxxxxxx> wrote:

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

and im certain it was used heavily that way in php4 as well.  its also
great for those people who dont want to use classes in their php.
i mainly avoid it as a matter of preference.  all im saying from earlier,
is that even if it does work inside class methods, theres no doc out there
that gives the green light on such usage (at least not to my knowledge).

-nathan

[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