|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-02-18 10:57 UTC] nikic@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: nikic
[2021-02-18 10:57 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 20:00:01 2025 UTC |
Description: ------------ Setup: a parent class which defines a static variable in a method. The static variable is initialized with NULL and set to some value on the first method call. Then, a child class calls the same (inherited) method. If the child class definition is in the same file as the parent class definition, this works as expected. However, if the child class definition is loaded from a separate file and this file is loaded after(!) the first call to the method which sets the static variable, the static variable seems to be "shared" between both classes. I'm really curious about an explanation in case this is not a bug because i would like to understand the concept. Thank you! Test script: --------------- class ParentClass { protected $something = 'parent'; public function getSomething() { static $cache; return $cache !== null ? $cache : $cache = $this->something; } } $parent = new ParentClass(); var_dump($parent->getSomething()); // expected: 'parent'; returned: 'parent' include 'child.php'; # content of child.php: //class ChildClass extends ParentClass //{ // protected $something = 'child'; //} $child = new ChildClass(); var_dump($child->getSomething()); // expected 'child'; returned 'parent'