2013-09-21 109 views
0

我想在這樣的方式一類公共/私有變量聲明中它

(非常精簡)

class Test extends Whatever 
{ 

    private $rules = array(
     'folder' => 'files/game/pictures/' . date('Ymd'), //this line causes error mentioned below 
    ); 

    public function __construct() {//some code} 

} 
設置 public/private(無所謂)變量(陣列)

,它給我一個錯誤

Parse error: syntax error, unexpected '.', expecting ')'

爲什麼呢?我一直宣稱這樣的數組並沒有問題。


解決方案:這個問題下面第一個評論。

+6

'聲明可能包括初始化,但初始化必須是一個恆定值 - 也就是說,它必須能夠在編譯時進行評估,並不能依賴於運行時信息爲了評估。 '[Object Properties](http://www.php.net/manual/en/language.oop5.properties.php) - 包含函數調用 –

+0

的結果哦謝謝,我在一小時內查找這個文檔 – Kyslik

回答

0

請嘗試:

class Test extends Whatever { 

    private $rules; 

    public function __construct() { 
     $this->rules = array(
      'folder' => 'files/game/pictures/' . date('Ymd'), //this line causes error mentioned below 
     ); 
    } 

} 
+0

只要我看到第1 [評論](http://stackoverflow.com/questions/18933728/public-private-variable-declaration-with-run-time-function-in-it/18933809?noredirect=1#comment27956773_18933728) ,我知道答案,但謝謝。 – Kyslik

0

類中聲明瓦爾不使用數組的(它是一個對象)。 試試這個:

class Test extends Whatever { 

    private $rules = array(); 

    public function __construct() { 
     $this->rules = array('folder' => 'files/game/pictures/' . date('Ymd')); 
     // Other code... 
    } 
} 
+0

只要我看到第1 [評論](http://stackoverflow.com/questions/18933728/public-private-variable-declaration-with-run-time-function-in-it/18933809?noredirect=1#comment27956773_18933728 ),我知道答案,但謝謝。 – Kyslik