考虑以下几点:
$var = 'foo' . 'bar'; # Not a member of a class, free-standing or in a function.
一旦我将 $var
标记为 static
,但是:
static $var = 'foo' . 'bar';
PHP(WAMP 设置上的 5.3.1)提示以下错误:
Parse error: syntax error, unexpected '.', expecting ',' or ';'
似乎字符串连接是这里的罪魁祸首。
这是怎么回事?谁能给我解释一下静态变量的规则?
最佳答案
手册指出, Variables scope :
Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.
Static keyword 中也提到了它:
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed.
尽管应该注意,无论是否为静态属性,都不能使用表达式进行初始化。
关于php - PHP 中的静态函数变量和连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4976717/