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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -