1

Okay, the post title might be a little confusing.

I have this code:

class A {
    public static foo() {
        return get_called_class();
    }
}

class B extends A {

}

class C {
    public function bar() {
        echo B::foo();
    }
}

Output: C

WHat I want to get in foo() is the class name of B. How can I do this without changes in the class B?

Regards, Jan Oliver

PS: __ CLASS __, get_class() are not working.

1

2 Answers 2

2

Before PHP 5.3, this is not possible without hacks and is known as late static binding, a googleable term.

If you're interested, here is the SO article with answers: Faking Late Static Binding before php 5.3

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

2 Comments

I read the docs of php, but I didn't find a solution for my problem, for I don't want to change the B class.
As I mentioned, if you're using php lower than 5.3 it's not possible without the hacks I provided: stackoverflow.com/questions/890505/…
0

I think you could use the built-in Reflection class for that.

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.