2009-07-31 181 views
13

是否可以从扩展类中获取顶级类的名称,而无需从顶级类中进行设置。看下面的例子,我想从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(); 

(PHP 5.2.4+)

回答

25

用途:

get_class($this); 
25

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

get_called_class(),贾森说,在PHP引入5.3

+1

`get_called_class()`只是因为5.3 – jason 2009-07-31 20:24:11

+5

可用我喜欢PHP 5.3。 – 2009-07-31 20:24:39

3

您可以简单地使用:

get_class($this);