3

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?

0

2 Answers 2

9

Instead of get_class(), use get_called_class().

Sign up to request clarification or add additional context in comments.

Comments

6

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.