3

说我有这门课

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();

我怎么知道是谁打来的?

4

1 回答 1

7

您正在寻找的是get_call_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();
    }
}

将输出被调用类的名称。

于 2013-04-01T08:17:15.893 回答