|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-08-10 06:29 UTC] magicaltux@php.net
[2009-08-10 07:09 UTC] colder@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 00:00:02 2025 UTC |
Description: ------------ In some cases the class name as returned by get_called_class() is incorrect. Can't explain this clearly without more sleep, but the following reproduction code should help understanding the problem. I tried to simplify the code, but the bug disappears in this case. Expected behaviour is to have class C constructed, then class B constructed, with a nice "Here is B\n" displayed. In reality the B::getInstance() static call done from C::__construct doesn't change the classname for LSB, meaning that when Singleton::getInstance() will call get_called_class() it will receive "C" (and letting us enter into an infinite recursion). Reproduce code: --------------- <?php abstract class Singleton { private static $singleton = array(); public function getInstance() { $class = get_called_class(); if (!isset(self::$singleton[$class])) { self::$singleton[$class] = new $class(); } return self::$singleton[$class]; } abstract protected function __construct(); } class B extends Singleton { protected function __construct() { echo "Here is B\n"; } } class C extends Singleton { protected function __construct() { B::getInstance(); } } C::getInstance(); Expected result: ---------------- Here is B Actual result: -------------- Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 523800 bytes) in foo.php on line 25