提问者:小点点

在PHP中使用哪个子类调用父类的方法?


假设我有以下场景。

class Base {
    public static function functionA() {
        // I want to know this method is called by which child class's instance
    }
}

class Child extends Base {
    public static function someFunc() {}
}

class AnotherChild extends Base {
    public static function someOtherFunc() {}
}

// Now I call the static function from the Base class
Child::functionA();
AnotherChild::functionA();

通过使用上面的示例,我如何检测通过使用childanotherchild类调用的方法functionA()。 有可能吗?


共1个答案

匿名用户

有不同的方法来获取类名,但我现在唯一的方法是获取被调用的实际类:

class Base {
    public static function functionA() {
        echo get_called_class();
    }
}