In php I have a ROOT class from which all other classes inherit.
abstract class ROOT{
public static function getClass(){
}
}
I want that function to return the class(name) of the object which inherits from this class. So if I create an object Tree (extends ROOT) and call getClass on it it should say "Tree"
The function get_class() only works on objects, so can't be used inside a static function. Is there any way to accomplish this?
Instead of get_class(), use get_called_class().
http://www.php.net/manual/en/function.get-called-class.php
abstract class ROOT {
public static function getClass() {
return get_called_class();
}
}
class Tree extends ROOT {
}
$Tree = new Tree();
echo $Tree->getClass(); // Outputs "Tree"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With