PHP 将值返回到新数组时出错
PHP Error when returning value into new array
我想使用另一个 class 的静态方法返回的值构建一个新数组,如下所示:
class Foo {
public static $vars = array(
'foo' => Config::method('returns a string'), /* line where error occurs */
'bar' => Config::anothermethod('another string')
);
}
现在 PHP 抛出此错误:
Parse error: syntax error, unexpected '(', expecting ')' in file.php on line 22
该方法只是 returns 一个字符串,我仔细检查过。我什至尝试用 (string) Config::method('foo')
对其进行字符串化,但没有成功。这不可能吗?
class 变量的声明必须是常量表达式。方法调用不是常量表达式。
http://php.net/manual/en/language.oop5.properties.php
This declaration may include an initialization, but this
initialization must be a constant value--that is, it must be able to
be evaluated at compile time and must not depend on run-time
information in order to be evaluated.
我想使用另一个 class 的静态方法返回的值构建一个新数组,如下所示:
class Foo {
public static $vars = array(
'foo' => Config::method('returns a string'), /* line where error occurs */
'bar' => Config::anothermethod('another string')
);
}
现在 PHP 抛出此错误:
Parse error: syntax error, unexpected '(', expecting ')' in file.php on line 22
该方法只是 returns 一个字符串,我仔细检查过。我什至尝试用 (string) Config::method('foo')
对其进行字符串化,但没有成功。这不可能吗?
class 变量的声明必须是常量表达式。方法调用不是常量表达式。
http://php.net/manual/en/language.oop5.properties.php
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.