late static binding | without modifying parent cla

2019-06-06 13:41发布

问题:

I have following parent and child class.

class Parent_class {

    protected static function method_one() {
        echo "I am in Parent_class in method_one";
    }

    protected function execute() {
        static::method_one();
    }

    public function start() {
        $this->execute();
    }

}

class Child_class extends Parent_class {

    protected static function method_one() {
        echo "I am in Child_class in method_one";
    }

}

$obj = new Child_class();
$obj->start();

Result - it is calling Child class method.

The result is as expected because of static late binding is supported in php5.3 with the already reserved keyword static.

But the issue is, I do not have write access to Parent class, hence I can not use static while calling methode_one and hence it is not performing late static binding.

Is there any way out using which I can access overriding method ?

Parent class is a defined library, and I can not modify it.

Way out is to modify the parent class or drop this thought completely, but can you suggest any other alternative ?

回答1:

Why not implement execute or start in child class?