在Array中使用句点(.)时发生PHP解析错误


PHP parse error when using dot (.) inside Array

当我运行以下代码时:

<?php
private $config = [
  'cacheFile' => 'a'.'b'
];

我收到:

Parse error: syntax error, unexpected '.', expecting ']' in ...

我的配置是:PHP版本5.5.9-1ubuntu4.14,服务器API FPM/FastCGI,nginx/1.4.6

我还用Nginx/Apache在localhost(OS X El Capitan)上测试了上面的代码,两个测试都通过了

知道问题出在哪里吗?非常感谢。

根据文档:

状态:在PHP 5.6 中实现

这个RFC将静态标量表达式引入解析器。这允许只接受静态值(const声明、属性声明、函数参数等)的地方也可以接受静态表达式。

因此,您的5.5无法做到这一点。

请注意,只有在编译时可以求值的表达式才能工作,因此

class foo {
   $x = 'a' . 'b'; // ok - can be calculated at compile-time
   $y = $_POST['foo']; // not ok - only calculable at runtime.
}