从扩展类中获取类名

Lou*_*s W 14 php oop

是否可以从扩展类中获取顶级类的名称,而无需从顶级类设置它.看下面的例子,我想从Base获得'Foo'.我知道我可以从Foo设置变量,但希望跳过额外的步骤.

谢谢.

class Base {

    function __construct() {

        echo '<p>get_class: '.get_class().'</p>';
        echo '<p>__CLASS__: '.__CLASS__.'</p>';

    }

}


class Foo extends Base {

}


$test = new Foo();
Run Code Online (Sandbox Code Playgroud)

(PHP 5.2.4+)

Tyl*_*ter 30

get_called_class()用于静态类或get_class($this)用于实例化.

get_called_class()正如杰森所说,在PHP 5.3中引入

  • 我喜欢PHP 5.3. (6认同)
  • 自PHP 5.5,你可以用'静:: class`,而不是`get_called_class()`和`get_class($这个)`. (2认同)

Gre*_*reg 5

你可以简单地使用:

get_class($this);
Run Code Online (Sandbox Code Playgroud)