php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49204 Late Static Binding class name problem
Submitted: 2009-08-10 06:14 UTC Modified: 2009-08-10 07:09 UTC
From: magicaltux@php.net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0 OS: Linux x86_64
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: magicaltux@php.net
New email:
PHP Version: OS:

 

 [2009-08-10 06:14 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-10 06:29 UTC] magicaltux@php.net
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".
 [2009-08-10 07:09 UTC] colder@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

No bug here, youre not calling getInstance() statically which means that get_called_class will basically return get_class($this);

In order for your code to work you need to mark getInstance as static.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 18:01:31 2025 UTC