mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 08:28:54 +00:00
[PHP 8.1] Usage of static Variables in Inherited Methods (#1549)
This commit is contained in:
parent
da9e1ca208
commit
61cfd00858
1 changed files with 29 additions and 1 deletions
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue