如何在不指定基 class 名称的情况下从 sub-sub-base-class 静态方法引用基 class 的父 class

How to refer to parent class of base class from sub-sub-base-class static method without specifying base class name

在PHP中,我想从子class中调用父class的父中的静态方法,而不引用父[=24] =]父class的名字(请看下面代码中的注释):

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 />

将父 class 名称存储在一个变量中,并使用该变量调用静态方法。像这样:

$parentClassName = get_parent_class(get_parent_class());
echo $parentClassName::helloStatic();