0

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?

NaN
  • 3,501
  • 8
  • 44
  • 77

1 Answers1

3
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

Community
  • 1
  • 1
Christoph Diegelmann
  • 2,004
  • 15
  • 26