[PHP 8.1] Usage of static Variables in Inherited Methods (#1549)

This commit is contained in:
Sergey Panteleev 2022-04-27 15:49:21 +03:00 committed by GitHub
parent da9e1ca208
commit 61cfd00858
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -485,7 +485,35 @@ function foo(){
]]>
</programlisting>
</example>
</para>
</para>
<para>
As of PHP 8.1.0, when a method using static variables is inherited (but not overridden),
the inherited method will now share static variables with the parent method.
This means that static variables in methods now behave the same way as static properties.
</para>
<example>
<title>Usage of static Variables in Inherited Methods</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo {
public static function counter() {
static $counter = 0;
$counter++;
return $counter;
}
}
class Bar extends Foo {}
var_dump(Foo::counter()); // int(1)
var_dump(Foo::counter()); // int(2)
var_dump(Bar::counter()); // int(3), prior to PHP 8.1.0 int(1)
var_dump(Bar::counter()); // int(4), prior to PHP 8.1.0 int(2)
?>
]]>
</programlisting>
</example>
<note>
<para>