后期静态绑定|而不用`static`关键字修改父类 [英] late static binding | without modifying parent class with `static` keyword

查看:137
本文介绍了后期静态绑定|而不用`static`关键字修改父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下父级和子级.

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.

结果如预期的那样,因为php5.3中已保留关键字static支持静态后期绑定.

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

但是问题是,我没有对Parent类的写权限,因此调用methode_one时不能使用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 ?

推荐答案

为什么不在子类中实现执行或启动?

Why not implement execute or start in child class?

这篇关于后期静态绑定|而不用`static`关键字修改父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆