Properties类中的名称

时间:2017-01-02 11:11:59

标签: php class

问题:获取类属性的名称。

gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.4.5
1 gem installed

如何获取属性中继承的类的名称?

1 个答案:

答案 0 :(得分:1)

这是因为,$name仅在父类中声明;所以它拥有父类的类名。如果需要子类名,请在子类中声明该变量。在这种情况下,它会覆盖父类的变量:

<?php

class Vasya {
    public $name = __CLASS__;
}

$class = new Vasya();
echo $class->name; // result Vasya

class Petro extends Vasya { 
    public $name = __CLASS__;
}

$class = new Petro();
echo $class->name; // result Petro