|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-12-02 11:49 UTC] tony2001@php.net
[2006-12-03 00:47 UTC] paul at digitalbacon dot us
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 02:00:01 2025 UTC |
Description: ------------ When using a static variable defined inside a parent class's static method block, different outcomes can occur depending on the scope name used when calling. The effect is that a non-overridden method in the child class is allocated a completely separate static variable... not how it works in C or C++, which I know PHP is not. I encountered this problem implementing a singleton pattern in a parent class. I apologize for the reproduction code, but it does illustrate my perceived problem. The closest bugs I found, relate to static class attributes, not regular static variables. Reproduce code: --------------- <?php class TheParent { public static function staticMethod() { static $theStaticValue = true; if ($theStaticValue == true) { $theStaticValue = false; return true; } else return $theStaticValue; // false } } class TheChild extends TheParent {} $a1 = TheParent::staticMethod(); $a2 = TheParent::staticMethod(); $b1 = TheChild::staticMethod(); $b2 = TheChild::staticMethod(); echo '$a1: ', $a1, "\n"; // true echo '$a2: ', $a2, "\n"; // false echo '$b1: ', $b1, "\n"; // true (I believe this should be false) echo '$b2: ', $b2, "\n"; // false ?> Expected result: ---------------- My intuition tells me that the parent's static variable should be used regardless of the scope name used... unless it's actually redefined in the child class. In this example, I expect the output to be: $a1: 1 $a2: $b1: $b2: Is local static method data not shared? Actual result: -------------- $a1: 1 $a2: $b1: 1 $b2: