2017-02-21 64 views
0

我試圖創建一個函數數組。雖然,執行此代碼在數組中使用函數時出現分析錯誤

protected $filter_functions = [ 
    "price" => function(&$query, $lower, $higher) { 
     $query->where("price", ">=", $lower)->where("price", "<=", $higher); 
    } 
]; 

當我越來越parse error,僅此而已,我不能看到任何語法錯誤,但也許有。 PHP版本目前是5.6.28,應該與匿名函數兼容。

+0

呢? – Sakezzz

+0

不,Laravel框架 –

+0

我向標籤添加了該信息。謝謝 –

回答

0

你必須你使用Yii投入構造

protected $filter_functions; 

public function __construct(){ 
    $this->filter_functions = [ 
     "price" => function(&$query, $lower, $higher) { 
      $query->where("price", ">=", $lower)->where("price", "<=", $higher); 
     } 
    ]; 
} 
+0

'__constructor'應該最有可能讀爲'__construct'。除非Laravel使用'__constructor'。編輯:這個評論按照原始http://stackoverflow.com/revisions/42370244/1 –

+0

https://laravel.com/docs/5.0/controllers它使用'__construct'好吧。 –

+0

我想在你嘗試設置'$ this-> filter_functions'之前,你還需要'parent :: __ construct();',但我不是100%。 –

相關問題