M. Sokolewicz wrote:
Frédéric hardy wrote:
Hello -
I have this code :
abstract class foo
{
private __construct() {}
public static getInstance()
{
static $instance = null;
if (is_null($instance) == false)
return $instance;
else
{
$class = __CLASS__;
return new $class();
}
}
}
class extendedFoo extends foo
{
...
}
$instance = extendedFoo::getInstance();
echo (get_class($instance));
My problem is that get_class($instance) return "foo" instead of
"extendedFoo" !!!
How to knwo the calling object type in a function called statically ?
Reflection ? Other solution ??? Bug ????
I can resolve my probleme with a extendedFoo::getInstance() function,
and adding a parameter $class to foo::getInstance(), but i don't want
duplicated code in all foo subclass...
Fred -
this is a completely expected result. What you're doing is:
statically calling function a() which is defined in a PARENT class.
Inside this parent class, the function calls __CLASS__, which is a magic
constant, holding the name of the class it's in AT THAT MOMENT, and the
class it IS in is the parent class!.
There is no easy way to do what you ask for. If you were using
non-static objects/functions, then you could use get_class($this), which
would work just fine. However, when using it statically, $this does not
exist... so...
debug_backtrace()?
Not really inteded for this purpose :)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php