user2650277 user2650277 - 1 year ago 77
PHP Question

Detect whether a subclass has called a parent class

Suppose i call the parent class of my subclass with

.How can i detect whether the parent class have been called by a subclass in the parent class

Answer Source

I don't know if I understood your question but my suggestion is to set a static variable that's true, in my case $CHILD, in the Child class then use get_called_class in the Parent class and then test the aforementioned static variable.

class Base {

    public function __construct() {
        $child = get_called_class();
            echo "Parent has being called";

class Child extends Base {

    public static $CHILD = true;

    public function __constructor()

$child = new Child();

Another approach would be to use debug_backtrace as @icecub suggested

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download