静态方法和继承中的get_class(php)

Ale*_*ome 15 php inheritance static

我们有一个代码

class ParentClass {
  public static function getName() {
    return get_class(self);
  }
}

class ChildClass extends ParentClass {
}

echo ParentClass::getName(); # => 'ParentClass'
echo ChildClass::getName(); # => 'ParentClass'
Run Code Online (Sandbox Code Playgroud)

如果我使用get_class($ this),则会得到相同的结果.也适用于self :: $ this,static :: $ this等

有没有为子类添加方法而获取子类名的任何方法?

dec*_*eze 27

你将不得不使用get_called_class,后期绑定.仅在PHP 5.3之后可用.