Proper way to access a static variable inside a string with a heredoc syntax?

后端 未结 2 963
青春惊慌失措
青春惊慌失措 2021-01-06 13:02

Lets say I have a static variable called $_staticVar in my class which I am trying to access like this. The variable has a member aString which has the string v

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-06 13:25

    I'm fairly certain you must use a local or imported variable for string interpolation. The easiest solution? Why, make it local of course:

        $_staticVar = self::$_staticVar; // or did you mean self::_staticVar? Not too clear on that.
    
        echo <<Something {$_staticVar->something} more of something 
    eos;

    As for the reasons your examples didn't work:

        echo <<Something self::$_staticVar->{$something} more of something 
    eos;

    Interpolates undefined variables $something and $_staticVar, which results in an empty string and a notice.

        echo <<Something {${self::$$_staticVar->{$something}}} more of something 
    eos;

    Interpolates the value of something that definitely doesn't exist and never will and it's all really confusing but you know it doesn't work.

提交回复
热议问题