0

I have the following hiearchy:

class A {
    public static function getClass() {
        return __CLASS__;
    }
}

class B extends A {}

class C extends B {}

without overriding getClass() in either B or C, I would like the following output:

echo A::getClass() // A
echo B::getClass() // B
echo C::getClass() // C

Currently, all of the above simply output A. How can I achieve the desired behavior?

2
  • In case of __CLASS__ did you try get_class($this)? Commented May 18, 2014 at 12:06
  • I cannot use $this since I do not have any instances - the methods are static. Commented May 18, 2014 at 12:06

1 Answer 1

5

Try this.

class A {
    public static function getClass() {
        return get_called_class();
    }
}

class B extends A {}

class C extends B {}

Here's the demo

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

1 Comment

I just hate it when there is a standard function which does exactly what I want, and I gloss right over it. Thank you so much for pointing this out.

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.