late static binding | without modifying parent class with `static` keyword

醉酒当歌 提交于 2019-12-23 01:44:20

问题


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?



来源:https://stackoverflow.com/questions/14273057/late-static-binding-without-modifying-parent-class-with-static-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!