使用_CLASS_从父级获取子类名

时间:2013-12-01 20:25:26

标签: php oop

基本上我要做的是

class aParent{
    public function name(){
        return __CLASS__;
    }
}


class aChild extends aParent{

}

$child = new aChild();
echo $child->name();

我需要Child代替Parent。这可能吗?当我明显从Parent类调用方法时,为什么它首先返回Child

3 个答案:

答案 0 :(得分:1)

使用功能get_called_class()代替_CLASS_

答案 1 :(得分:1)

只需使用函数get_class($object),而不是在/ each类中创建和使用方法。

但您也可以在父类中创建一个方法,并使用get_class()而不需要任何参数。

答案 2 :(得分:1)

__CLASS__是“编译”时间常量,这就是为什么它会在脚本执行开始时用“父”替换Magic constants

  

此常量返回声明的类名

要获取当前的类名,您可以使用get_class函数

相关问题