问题描述
我有以下父级和子级.
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`关键字修改父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!