這裏是一個最小的測試用例我隔離:PHP類屬性使用範圍()
<?php
class What {
public $foo = range(0,5);
}
?>
我不知道爲什麼這會產生一個錯誤:
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestCase.php on line 4
使用array()
作品。
使用PHP 5.3.3(與OS X捆綁)。
這裏是一個最小的測試用例我隔離:PHP類屬性使用範圍()
<?php
class What {
public $foo = range(0,5);
}
?>
我不知道爲什麼這會產生一個錯誤:
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestCase.php on line 4
使用array()
作品。
使用PHP 5.3.3(與OS X捆綁)。
您只能在該上下文中分配常量值。如果要使用函數的返回值,則必須在構造函數中初始化$foo
。
<?php
class What {
public $foo;
public function __construct() {
$this->foo = range(0,5);
}
}
?>
順便說一句:正如其他人指出的,array()
不是一個函數。這是一種語言結構。
數組不是函數,它是一種語言結構。這就是允許它的原因。
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.
檢查此鏈接爲一個類似的問題被髮送,已經得到了解決: - 在PHP
http://www.phpbuilder.com/board/showthread.php?t=10366062
還可以看到使用範圍的例子()函數.Net手冊,儘管我認爲問題可能出現在變量$ foo和public關鍵字中,因爲您可能會遇到類型不匹配或PHP函數版本與正在運行的PHP版本衝突的情況。
我希望這個答案可以幫助你..
你的評論,「許多其他功能的工作」不能是正確的。 – Matthew 2010-12-07 06:10:07
@konforce故事的寓意在於,不要在深夜問到超級堆棧溢出問題。 D: – Karew 2010-12-08 23:43:03