php用其他静态变量初始化静态变量

时间:2013-08-23 08:56:42

标签: php variables static-variables

我尝试使用另一个的内容初始化静态var,但似乎失败了。

class Hey {
    static $user = "peter";
    static $home = '/home'.Hey::$user;

    // syntax error, unexpected T_VARIABLE, expecting T_STRING

为什么它会失败并且没有init-function或其他东西?

1 个答案:

答案 0 :(得分:3)

class Hey {
    static $user = "peter";
    static $home;
}
Hey::$home =  '/home'.Hey::$user;

或$ home是私有的:

class Hey {
    static $user = "peter";
    private static $home;
    static function init(){self::$home = '/home'.self::$user;}
}
Hey::init();

请参阅How to initialize static variables