php - 哪个孙子叫我?

标签 php late-static-binding

假设我有这个类(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/

相关文章:

php - 此路由不支持 POST 方法。 (Laravel 与 ajax)

php - 我应该使用 new self 还是 new static?

php - 如何从父静态函数调用静态子函数?

php - 有没有办法让 PHP 子类继承属性(静态和实例)?

php - 是否有可能在 PHP 中过度使用后期静态绑定(bind)?

php - 如何在 php 7.1 中指示返回类型是当前子类型?

php - 插入mysql时 undefined variable

php - 如何在 PHP 中集成 Assently api 进行电子签名交易

PHP Microsoft Excel 文件生成/导出类

php - CDb命令 : reduce value of column itself