有时我需要执行grandparent方法(也就是绕过父方法),我知道这是代码味道,但有时候我不能改变其他类(框架,库等).
在PHP中,我们可以通过以下方式实现:
call_user_func(array(get_parent_class(get_parent_class($childObject)), 'grandParentMethod'));
问题是,如果您启用了E_STRICT错误,您将收到如下错误:
严格的标准:非静态方法GrandParent :: grandParentMethod()不应该在…中静态调用
我发现只有一个解决方案(没有删除E_STRICT),它只是添加@来抑制错误.
但这真的很难看,有人知道更好的解决方案吗?
谢谢 !
PS:我无法实例化一个新对象,如:
$grandparent = get_parent_class(get_parent_class($son));
$gp= new $grandparent;
$gp->grandParentMethod
因为我需要在$son的上下文中调用我的祖父母方法.
解决方法:
例:
class Grandpa {
protected $age = 'very old';
public function sayMyAge() {
return 'sayMyAge() in Grandpa should be very old. ' .
'My age is: ' . $this->age;
}
}
class Pa extends Grandpa {
protected $age = 'less old';
public function sayMyAge() {
return 'sayMyAge() in Pa should be less old. ' .
'My age is: ' . $this->age;
}
}
class Son extends Pa {
protected $age = 'younger';
public function sayMyAge() {
return 'sayMyAge() in Son should be younger. ' .
'My age is: ' . $this->age;
}
}
$son = new Son();
$reflectionMethod = new ReflectionMethod(get_parent_class(get_parent_class($son)),
'sayMyAge');
echo $reflectionMethod->invoke($son);
// returns:
// sayMyAge() in Grandpa should be very old. My age is: younger
注意:调用的方法必须是公共的.
标签:php,inheritance
来源: https://codeday.me/bug/20191007/1869321.html