方法中的静态变量不会在实例未设置时重置

Static variable in method doesn't reset on instance unset

PHP 5.6.12-arm 和 PHP 7 RC3 中的有趣行为(虽然我猜它在所有版本中都是这样,我只是想记下我用来测试的版本):

示例 - 在 class 方法中使用静态变量

<?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 - but why?
print PHP_EOL;
?>

问题:如何打印 2,因为我们在再次调用 Foo->Bar() 之前取消设置了整个实例?

请注意,this 问题及其答案并未回答我的问题。

此致。

您可以查看 variables scope 的 php 文档。

if you declare a variable as static inside a function, it's static for the whole class and all of its instances, not for each object.

因此,静态变量与单个实例无关。