Static Function Variables and Concatenation in PHP

前端 未结 3 1372
无人及你
无人及你 2020-12-20 11:32

Consider the following:

$var = \'foo\' . \'bar\'; # Not a member of a class, free-standing or in a function.

As soon as I mark $var

3条回答
  •  没有蜡笔的小新
    2020-12-20 12:21

    You can not do expressions in initializers. You can, however, do this:

    define('FOOBAR', 'foo'.'bar');
    static $var = FOOBAR;
    echo $var;
    

    Little known fact is that even though initializers can not contain runtime expressions, it can contain constants which can be defined and resolved at runtime. The constant has to be defined by the time $var is first used though, otherwise you'll get string identical to the constant (e.g. "FOOBAR").

提交回复
热议问题