문제

I tried to initialize a static var with the content of another one and it seems to fail.

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

    // syntax error, unexpected T_VARIABLE, expecting T_STRING

Why does it fail and is there a way without an init-function or something else?

도움이 되었습니까?

해결책

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

or if $home is private:

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

see How to initialize static variables

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top