在PHP中,我想从子子类中调用父类的父类中的静态方法,而不引用父类的父类的名称(请参阅中的注释)代码如下):
class Base {
public static function helloStatic() {
return "Hello base!\n";
}
}
class Foo extends Base {
private $fooMember;
public static function helloStatic() {
return "Hello foo!\n";
}
private function __construct() {
$this->fooMember = "hello";
}
public function getFooMember() {
return $this->fooMember;
}
}
class Bar extends Foo {
private $barMember;
public static function helloStatic() {
// I want to write the equivalent of:
//echo Base::helloStatic();
// here *without specifying any class names*
echo get_parent_class(get_parent_class())::helloStatic();
}
}
echo Bar::helloStatic();
预期输出:
Hello base!
输出:
<br />
<b>Parse error</b>: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' on line <b>45</b><br />
答案 0 :(得分:2)
将父类名存储在变量中,并使用该变量调用静态方法。像这样:
$parentClassName = get_parent_class(get_parent_class());
echo $parentClassName::helloStatic();