Re: Object Inheritance, get_class() and a static function call

From: Date: Tue, 26 Jul 2005 21:48:49 +0000
Subject: Re: Object Inheritance, get_class() and a static function call
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-219385@lists.php.net to get a copy of this message
Hello Jochem,
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.php
http://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 API
no, no generic code. ;) regards axel

« previous php.general (#219385) next »