1
在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!
OUTPUT:
<br />
<b>Parse error</b>: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' on line <b>45</b><br />
有趣的解決方案。如果您也碰巧知道單線解決方案,請告訴我。謝謝。 – 2015-02-09 11:05:32