[英]PHP Class Property Using range()
Here is a minimal test case I've isolated: 这是我隔离的最小测试用例:
<?php
class What {
public $foo = range(0,5);
}
?>
I have no idea why this produces an error: 我不知道为什么会产生错误:
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestCase.php on line 4 PHP Parse错误:语法错误,意外'(',期待','或';'在第4行的TestCase.php中
Using array()
works. 使用array()
工作。
Using PHP 5.3.3 (bundled with OS X). 使用PHP 5.3.3(与OS X捆绑在一起)。
You can only assign constant values in that context. 您只能在该上下文中指定常量值。 You'll have to initialize your $foo
in a constructor if you want to use the return value of a function. 如果要使用函数的返回值,则必须在构造函数中初始化$foo
。
<?php
class What {
public $foo;
public function __construct() {
$this->foo = range(0,5);
}
}
?>
BTW: As others have pointed out, array()
is not a function. BTW:正如其他人所指出的那样, array()
不是一个函数。 It's a language construct. 这是一种语言结构。
Array isn't a function, it's a language construct. 数组不是函数,它是一种语言结构。 That's why it's allowed. 这就是它被允许的原因。
Class member variables are called "properties" ... 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. 类成员变量称为“属性”...可能包括初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时进行评估,并且不能依赖于运行时信息的顺序待评估。
http://www.php.net/manual/en/language.oop5.properties.php http://www.php.net/manual/en/language.oop5.properties.php
Check this link as a similar problem was sent and had been solved :- 检查此链接是否已发送类似问题并已解决: -
http://www.phpbuilder.com/board/showthread.php?t=10366062 http://www.phpbuilder.com/board/showthread.php?t=10366062
Also you can see the examples of using range() function in PHP.Net Manual although I think the problem may be in the variable $foo and public keyword as you there may be a type mismatch or a conflict beteen the version of the PHP function and your running PHP version. 您也可以在PHP.Net手册中看到使用range()函数的示例,虽然我认为问题可能在变量$ foo和public关键字中,因为您可能存在类型不匹配或PHP函数版本之间的冲突和你正在运行的PHP版本。
I hope this answer helps you.. 我希望这个答案对你有所帮助..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.