#49204 [Opn]: Late Static Binding class name problem
| From: | magicaltux@php.net | Date: | Mon, 10 Aug 2009 06:29:37 +0000 |
| Subject: | #49204 [Opn]: Late Static Binding class name problem | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-140837@lists.php.net to get a copy of this message | ||
ID: 49204
Updated by: magicaltux@php.net
Reported By: magicaltux@php.net
Status: Open
Bug Type: Scripting Engine problem
Operating System: Linux x86_64
PHP Version: 5.3.0
New Comment:
I tried duplicating the base "Singleton" class into "Singleton2", and
make C extend Singleton2, and the problem still happens.
Fatal error: Call to protected C::__construct() from context
'Singleton' in /home/magicaltux/foo.php on line 9
The right context is called (class "Singleton") but get_called_class()
still returns "C" instead of "B".
Previous Comments:
------------------------------------------------------------------------
[2009-08-10 06:14:07] magicaltux@php.net
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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49204&edit=1