Re: Re: [PHP5] How to knwo object class name in a function
| From: | M. Sokolewicz | Date: | Thu, 07 Oct 2004 09:39:33 +0000 |
| Subject: | Re: Re: [PHP5] How to knwo object class name in a function | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-198814@lists.php.net to get a copy of this message | ||
Marek Kilimajer wrote:
M. Sokolewicz wrote:Frédéric hardy wrote:debug_backtrace()? Not really inteded for this purpose :) that's a possibility... surely not the Reflection API though =/ it only works on static objects AFAIK (or new instances)Hello - I have this code : abstract class foo { private __construct() {} public static getInstance() {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...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 -