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?

1
  • 1
    guys don't fool him ?! it's simply not possible as initializions must be constant they are not allowed to use variables at all! Commented Aug 23, 2013 at 9:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.