哪位孙子打电话给我?

时间:2013-04-01 08:03:59

标签: 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
{
}

现在,我像这样调用Grandson类中的函数:

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

我怎样才能找出谁叫了?

1 个答案:

答案 0 :(得分:7)

您正在寻找的是get_called_class功能。从PHP文档: -

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

所以

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

将输出被调用类的名称。