Re: [PHP-DEV] Bug #331: static variables in object methods broken From: Zeev Suraski (bourbon <email protected>)
Date: 04/30/98

At 09:52 30/04/98 +0200, Andreas Braukmann wrote:
>Hello,
>
>On Thu, Apr 30, 1998 at 02:39:21AM +0000, Zeev Suraski wrote:
>> >> I think if we document the fact that the static is shared among that
class
>> >> of objects, it is better than simply disallowing it. Who knows, maybe
>> >> this is useful to someone.
>> >definitely. I would appreciate to have some kind of class variables.
>> >From the oo point of view, they _are_ useful. One shouldn't compare
>> >them to anything ugly as real global variables ....
>> But that's almost exactly what they are..?
>Oooops. Sorry, I have to apologize. I was simply to eager to have
>something like real class-variables. By that I oversought that the
>discussion was on "static variables" local to member functions and not
>on "static class members".
>According to this, I have to second your opinion, that this feature
>isn't of to much value.

Well, static class varibles aren't all that different from static variables
inside member functions. You can write an implementation for a general
static class-wide variable using a member function like this:

class foo {
        ...
        function static_var($op="GET",$operand=0) {
                static $stat = 0;

                switch ($op) {
                        case "SET":
                                $stat = $operand;
                                /* break missing intentionally */
                        case "GET":
                                return $stat;
                                break;
                        default:
                                echo "Warning: Unsupported op - '$op'";
                                break;
                }
        }
}

That way you practically have a class-wide variable, that you can set using
$obj->static_var("SET",$value) and get using $obj->static_var().

Zeev

--
Zeev Suraski   <zeev <email protected>>
For a PGP public key, finger bourbon <email protected>