note 118079 deleted from language.variables.scope by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 19:34:52 +0000 |
| Subject: | note 118079 deleted from language.variables.scope by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217923@lists.php.net to get a copy of this message | ||
Note Submitter: nino dot skopac at gmail dot com
----
Interesting behavior in PHP 5.6.12 and PHP 7 RC3:
<?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
print PHP_EOL;
?>
How can a 2 be printed, since we unseted the whole instance before?
Consider a similar example:
<?php
class Foo {
public static $var = 0;
public static function Bar() {
return ++self::$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
print PHP_EOL;
?>
No idea why is this happening.