Is there some method in PHP to get the name of whatever class you’re currently in, when using only static methods? I’m thinking if there is, it probably has something to do with Reflection, but I’m not sure.
To illustrate, here’s what I like to do:
class MyClass
{
public static function getName()
{
// do something here, yielding the string variable $name
return $name;
}
}
echo MyClass::getName();
This should output MyClass. Is there some way to do this? (This might seem silly, but that’s only because I’ve broken down what I need into an oversimplified example.)
public static function getName() {
return __CLASS__;
}
However this won’t work if you extend MyClass.
class NewClass extends MyClass...
echo NewClass::getName(); // still says MyClass
You have to wait for php 5.3 and its “late static binding” feature for this to work properly. In the meantime, a hack based on [fphp]debug_backtrace[/fphp] seems to be the only choice.