ID:               49204
 Updated by:       col...@php.net
 Reported By:      magical...@php.net
-Status:           Open
+Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: Linux x86_64
 PHP Version:      5.3.0
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[2009-08-10 06:29:36] magical...@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 06:14:07] magical...@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

Reply via email to