4

I have this code in PHP:

abstract class Development
{   
    static function testUnit()
    {
        echo get_class();
    }
}

class Component extends Development
{    
}

But if I am calling Component::testUnit();

I am receiving Development instead of Component. It is puzzling me because such static function has no meaning I guess.

2 Answers 2

7

You want to use get_called_class instead ... http://php.net/manual/en/function.get-called-class.php

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

Comments

3

As @Jason at FloSports said, you can use get_called_class, with that there is 1 more ways, you can get current called class name and i.e. echo get_class($this);

Since, you are already using get_class(), you just have to add $this. So, that it could point current class. Here's an attached link that will help you understand between these 2 functionsSee, if that helps.

2 Comments

Thank you very much both of you. Jason is right, "get_called_class" was the correct function. But adding $this solves nothing because the method is static.
True, get_class($this) will not work on static class

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.