检测子类是否调用了父类


Detect whether a subclass has called a parent class

假设我用parent::__construct();调用子类的父类。我如何检测父类是否已被父类中的子类调用

我不知道我是否理解你的问题,但我的建议是设置一个静态变量,即true,在我的情况下$CHILD,在子类然后在父类中使用get_called_class,然后测试上述静态变量。

class Base {
    public function __construct() {
        $child = get_called_class();
        if($child::$CHILD)
        {
            echo "Parent has being called";
        }
    }
}
class Child extends Base {
    public static $CHILD = true;
    public function __constructor()
    {
        parent::__constructor();
    }
}
$child = new Child();

另一种方法是像@icecub建议的那样使用debug_backtrace