문제

Say I have this classes

class Grandpa
{
    public function call(){
        // Well, I want to know who calls me here
    }
}

class Father extends Grandpa
{
}

class GrandsonOne extends Father
{
}

class GrandsonTwo extends Father
{
}

Now, I call functions from the Grandson classes like so:

GrandsonOne::call();
GrandsonTwo::call();

How can I figure out who called?

도움이 되었습니까?

해결책

What you're looking for is the get_called_class function. From the PHP docs:-

Gets the name of the class the static method is called in.

So

class Grandpa
{
    public function call()
    {
        // Well, I want to know who calls me here
        echo get_called_class();
    }
}

will output the name of the called class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top