Re: Static method variable

From: Date: Thu, 18 Sep 2008 19:01:49 +0000
Subject: Re: Static method variable
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-280455@lists.php.net to get a copy of this message
Christoph Boget schreef:
Perhaps I'm misunderstanding what a static method variable is supposed to do. I thought the value would be static for an class' instance but it appears it is static across all instances of the class. Consider: class StaticTest { public function __construct() { } public function test( $newVal ) {
    static $retval = '';
    if( $retval == '' )
    {
      $retval = $newVal;
    }
    echo $retval . '<br>';
} } $one = new StaticTest(); $one->test( 'joe' ); $two = new StaticTest(); $two->test( 'bob' ); Should it be working that way?
yes. it's a function variable that retains state. everyone misread your opst so far ... this has nothing to do with static class scope, although it does work in the same way. you can do the exact same thing with a normal function: function test( $newVal ) { static $retval; if(!isset($retval)) $retval = $newVal; echo $retval . "\n"; } test('joe'); test('bob');
thnx, Chris


« previous php.general (#280455) next »