note 127364 added to language.oop5.late-static-bindings
| From: | 5imun.github.com via php-notes | Date: | Fri, 29 Jul 2022 07:38:25 +0000 |
| Subject: | note 127364 added to language.oop5.late-static-bindings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219078@lists.php.net to get a copy of this message | ||
Example of setting up static property in child class from parent only if it isn't already
defined, many people would expect that output will be "Foo Bar" but instead we get
"Foo Foo":
<?php
class Foo
{
public static string $A;
public static function init() {
return "Foo";
}
public static function get() {
if (!isset(static::$A)) {
static::$A = static::init();
}
return static::$A;
}
}
class Bar extends Foo {
public static function init() {
return "Bar";
}
}
$foo = new Foo();
$bar = new Bar();
echo $foo->get();
echo $bar->get();
?>
Output:
Foo
Foo
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 213.147.120.136)
----
Manual Page -- https://php.net/manual/en/language.oop5.late-static-bindings.php
Edit -- https://main.php.net/note/edit/127364
Del: integrated -- https://main.php.net/note/delete/127364/integrated
Del: useless -- https://main.php.net/note/delete/127364/useless
Del: bad code -- https://main.php.net/note/delete/127364/bad+code
Del: spam -- https://main.php.net/note/delete/127364/spam
Del: non-english -- https://main.php.net/note/delete/127364/non-english
Del: in docs -- https://main.php.net/note/delete/127364/in+docs
Del: other reasons-- https://main.php.net/note/delete/127364
Reject -- https://main.php.net/note/reject/127364
Search -- https://main.php.net/manage/user-notes.php