访问子类中抽象类的静态变量

时间:2012-11-05 11:15:29

标签: php class static

return 'Static hello ' . self::static_user . '...<br />'; 制作: Fatal error: Undefined class constant 'static_user' in ....

注意:逻辑可能很愚蠢,但我现在只是在玩它。问题是如何在子类中访问抽象类的静态变量?我可以在子类中使用$public_user访问公开的$this->public_user

abstract class UserAbstract
{
protected $public_user = null;
    static protected $static_user = null;

    public function __construct($name)
    {
    $this->public_user = $name;
        self::$static_user = $name;
    }

    abstract static function static_hello();
}

class User extends UserAbstract
{
    static function static_hello()
    {
        return 'Static hello ' . self::static_user . '...<br />';
    }
}

$obj_user = new User('Voodoo');
echo $obj_user->static_hello();

1 个答案:

答案 0 :(得分:2)

使用此:

return 'Static hello ' . parent::$static_user . '...<br />';