Re: Strange behaviour of static declared content.

From: Date: Thu, 18 Oct 2007 12:20:55 +0000
Subject: Re: Strange behaviour of static declared content.
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-263299@lists.php.net to get a copy of this message
Stut wrote:
Joshua Bacher wrote:
    I can't touch the API.
i have never use static variables inside of functions and likely never will. go for static class variables instead :) <?php class foo{
        public static $foobar = false;
        public function bar(){
                static $foobar=false;
                if (self::$foobar === False){
                        self::$foobar='FUBeyondAllR';
                        echo self::$foobar . "\n";
                }else{echo "already defined\n";}
        }
} $f=new foo(); $f->bar(); $f->bar(); foo::$foobar=false; $f->bar(); ?> nathan@devel ~/working/www/siuConference $ php testScript.php FUBeyondAllR already defined FUBeyondAllR -nathan
thats the solution for the wrong problem. it's not up to me to change the API. the API is designed like i noted and i need a way to get around this behaviour. thanks for your idea any further suggestions?
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. I think you're out of luck. You need to find a way to override this behaviour. Can you not add a function to the API to reset that static? That wouldn't break the existing contract but would let you do what you need. -Stut -- http://stut.net/

« previous php.general (#263299) next »