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
我也在 localhost(OS X El Capitan) 上用 Nginx/Apache 测试了上面的代码,两个测试都通过了。
知道问题出在哪里吗?谢谢。
根据 docs:
Status: Implemented in PHP 5.6
This RFC brings static scalar expressions to the parser. This allows places that only take static values (const declarations, property declarations, function arguments, etc) to also be able to take static expressions.
因此您的 5.5 无法执行此操作。
请注意,只有可以在编译时求值的表达式才有效,所以
class foo {
$x = 'a' . 'b'; // ok - can be calculated at compile-time
$y = $_POST['foo']; // not ok - only calculable at runtime.
}
当我运行以下代码时:
<?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
我也在 localhost(OS X El Capitan) 上用 Nginx/Apache 测试了上面的代码,两个测试都通过了。
知道问题出在哪里吗?谢谢。
根据 docs:
Status: Implemented in PHP 5.6
This RFC brings static scalar expressions to the parser. This allows places that only take static values (const declarations, property declarations, function arguments, etc) to also be able to take static expressions.
因此您的 5.5 无法执行此操作。
请注意,只有可以在编译时求值的表达式才有效,所以
class foo {
$x = 'a' . 'b'; // ok - can be calculated at compile-time
$y = $_POST['foo']; // not ok - only calculable at runtime.
}