假设我有这个类(class)
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();
我怎么知道是谁打来的?
最佳答案
您正在寻找的是 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();
}
}
将输出被调用类的名称。
关于php - 哪个孙子叫我?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739847/