【发布时间】:2012-12-25 17:36:39
【问题描述】:
我有以下父子类。
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。
但问题是,我没有对 Parent 类的写入权限,因此在调用 methode_one 时我不能使用 static,因此它没有执行后期静态绑定。
有什么方法可以访问覆盖方法吗?
父类是一个定义好的库,我不能修改它。
出路是修改父类或完全放弃这个想法,但你能提出其他替代方案吗?
【问题讨论】: