php - Static variable in method doesn't reset on instance unset -
interesting behavior in php 5.6.12-arm , php 7 rc3 (though guess it's in version, wanted note versions i've used test):
example - using static variable inside class method
<?php class foo { public function bar() { static $var = 0; return ++$var; } } $foo_instance = new foo; print $foo_instance->bar(); // prints 1 print php_eol; unset($foo_instance); $foo_instance2 = new foo; print $foo_instance2->bar(); // prints 2 - why? print php_eol; ?>
question: how can 2 printed, since unseted whole instance before calling foo->bar() again?
please note this question , answers don't answer question.
best regards.
you can in php documentation of variables scope.
if declare variable static inside function, it's static whole class , of instances, not each object.
so, static variable not related single instance.
Comments
Post a Comment