【问题标题】:access to static variable of abstract class in child class访问子类中抽象类的静态变量
【发布时间】:2012-11-05 11:15:29
【问题描述】:

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

注意:逻辑可能很愚蠢,但我现在只是在开玩笑。问题是如何访问子类中抽象类的静态变量?我可以在子类中使用$this->public_user 访问公共$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();

【问题讨论】:

    标签: php class static


    【解决方案1】:

    使用这个:

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

    【讨论】:

    • 我不敢相信自己。我怎么会错过......总是一些小事。我实际上知道我必须使用 $ 标记,但有时会发生,我能说什么......谢谢
    • 我正在尝试使用User::static_hello();,但不打印任何内容。甚至没有错误。
    • @MadMax : 为什么当这个函数只返回值时它会打印任何东西??
    • 我想我该去睡觉了……这次错过了echo ;)
    猜你喜欢
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2012-01-02
    相关资源
    最近更新 更多