2
votes

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?

2

2 Answers

7
votes

Instead of get_class(), use get_called_class().

5
votes

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"