哪位孙子打电话给我?

Mic*_*ael 3 php late-static-binding

说我有这个课程

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
{
}
Run Code Online (Sandbox Code Playgroud)

现在,我调用Grandson类中的函数,如下所示:

GrandsonOne::call();
GrandsonTwo::call();
Run Code Online (Sandbox Code Playgroud)

我该怎么弄清谁叫谁?

Sul*_*n C 7

您正在寻找的是get_called_class函数.来自PHP文档: -

Gets the name of the class the static method is called in.
Run Code Online (Sandbox Code Playgroud)

所以

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

将输出被调用类的名称.