Name of calling class using static methods
Posted: Mon Nov 05, 2007 2:01 pm
I'm trying to find the name of the current class when calling a static method. To illustrate, let me define a few classes:
Any ideas on how to get the desired behavior? Being able to do this would really "DRY up" my code, but I can't seem to find a way to do it.
Code: Select all
abstract class AbstractClass
{
public function foo()
{
return class_name($this);
# class_name(); gives just "AbstractClass"
}
public static function bar()
{
return class_name($this);
}
}
class ConcreteClassOne extends AbstractClass
{
public function test_non_static()
{
return foo(); # gives "ConcreteClassOne"
}
public static function test_static()
{
# doesn't work because there's not an instance of the class, obviously
# However, I would like to find a way of getting "ConcreteClassOne" as the return value in this context
return bar();
}
}
class ConcreteClassTwo extends AbstractClass
{
public function test_non_static()
{
return foo(); # gives "ConcreteClassTwo"
}
public static function test_static()
{
# See above
return bar();
}
}