类从具有相同专有名称的抽象类扩展而来


Class extend from abstract class with same propriety name

所以这是我的类声明

abstract class person {
    const NAME='person';
    public static function get_name(){
        return self::NAME;
    }
    abstract public function get_description();
}
class me extends person{
    const NAME = "me";
    public function get_description(){
        return "this describe " . self::NAME;
    }
}

正如您所看到的,const名称在Classes person和me中都声明了两次。我在类"person"中声明它,因为它就在我们在方法get_name()的实现中使用它的地方我在课堂上也声明它为"我",因为我想得到"我"的名字。

所以当我打电话给时

echo me::get_name() 

我希望它返回"我"

实际上,它返回的是"人",所以我在这里缺少什么,所以它会返回"我"。

感谢

将抽象方法更改为:

abstract class person {
    public static function get_name(){
        return static::NAME;
    }
}

这将在PHP中使用后期静态绑定,如文档中所述:http://php.net/manual/en/language.oop5.late-static-bindings.php