>> The second case is referencing the varible of the class. Maybe you are right. However, I don't really think that there is a true "reference" to the class var in example #2. PHP documentation of static keywords does not unambiguously explain behavior of "static" variables inside methods in example #1. I believe that in example #1 the exactly same instance of function (method) is used irregarding of how you call it (X::test() or Y::test()), therefore I would expect the same "static" var to be involved in both calls to test(). Is there anybody who can comment on this matter? Thanks. 6 июля 2011 г. 11:05 пользователь Andrew Williams <andrew4williams@xxxxxxxxx > написал: I think you are confusing scope visibility level of the variable within > method and the class. > > Variable within the method is going to 1 because it was declare within the > test method and there no link to the one declared outside the test method. > The second case is referencing the varible of the class. > > > > 2011/7/6 Дмитрий Степанов <dmitrij@xxxxxxxxxxx> > > > Hello, everybody. > > > > While working with static variables inside static class' methods, I have > > found this very interesting (at least for me) behavior of PHP. > > > > Consider the following class definitions (example #1): > > > > class X { > > public final static function test() { > > static $i; > > return ++$i; > > } > > } > > > > class Y extends X { > > } > > > > By executing this code: > > > > echo X::test(); > > echo Y::test(); // note Y class here > > > > one would expect to see "12" as output, but apparently I get "11". > > > > That's a bit confusing if you logically assume that "static vars" are > "tied > > to the scope" they're defined in. Since this static variable is > > defined in a specific static method test(), that is NOT overloaded by > class > > Y, in my opinion it shoul've preserved it's value across static calls. > > > > Let's look at another example (example #2): > > > > class X { > > public static $x =0; > > public final static function test() { > > return ++static::$x; // note static keyword here > > } > > } > > > > class Y extends X { > > } > > > > If you run this code: > > > > echo X::test(); > > echo Y::test(); > > > > you get "12" as output - the expected output. Notice that the > > "++static::$x" > > expr. is taking advantage of late static binding. Now, if you change > > body of test() to the following code: > > > > public final static function test() { > > return ++self::$x; > > } > > > > then you also get "12" as output. > > > > Is this a bug that static context of $i is not preserved in example #1 or > > do > > I misunderstand something? > > > > I could not find any hints on this in the PHP documentation. > > > > Dmitry. > > > -- Dmitry Stepanov E-mail: dmitrij@xxxxxxxxxxx Home: http://www.stepanov.lv Skype: ninzjoo -- Dmitry Stepanov E-mail: dmitrij@xxxxxxxxxxx Home: http://www.stepanov.lv Skype: ninzjoo