Re: Object Inheritance, get_class() and a static function call
| From: | Jochem Maas | Date: | Wed, 27 Jul 2005 08:44:03 +0000 |
| Subject: | Re: Object Inheritance, get_class() and a static function call | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-219400@lists.php.net to get a copy of this message | ||
axel wrote:
Hello Jochem,Axel lets start again. you seem to want to write a method in 1 base class that will give you the correct classname when you call the method statically on the/a subclass) ... In all cases It _seems_ to me that you must already know the name of the class in order to get to the stage that you want to call the getClassName() method. SO: explain, please, why and what you are trying to do (DETAILS!). don't dumb it down because that just makes the problem look moot. I suspect that you will need to change you code to attack to the problem from a different angle. anyway I'm very interested in the general problem so lets work it! rgds
either you tested some different code to what you posted above OR (your build of?) 5.0.3 is borked.sorry, I didn't tested the code at all, because my code is more complex. I just tried to show the problem. class ClassA { public static function getClassName() {return get_class($this); // [1]// or [2] return get_class(self); // or [3] return __CLASS__; } } class ClassB extends ClassA { } echo ClassB::getClassName(); I am searching for a function to use in line [1] so that the script's output is "ClassB".I tested this on 5.0.2 and 5.0.4 and both did exactly what I expected, namely output nothing - $this is not defined when you call ClassB::getClassName() (because you make a static call) so nothing is the only logical output.my fault, again, sorry. I jumbled the different outputs. I understood that this behaviour is coherent.maybe you mean: class ClassA {function getClassName() { return get_class($this); }} class ClassB extends ClassA {} $B = new ClassB(); echo $B->getClassName(); which _does_ work but is fairly pointless because you already know the classname in order to be able to create the object in the first place.that's true. I don't want to use this. I just checked it.I don't really see what the problem is, can you give some more real world detail on what you are trying/want to do? otherwise (re-)read this chapter: http://php.belnet.be/manual/en/language.oop5.phphttp://php.belnet.be/manual/en/language.oop5.static.php#48234 is better. but it seems that even debug_backtrace() is not what i'm looking for...... if you're _really_ into generic code then the reflection APIno, no generic code. ;) regards axel