我無法將函數聲明爲數組。這是我的代碼,我做錯了什麼?將函數添加到數組中php
private function array_list(){
return array('1'=>'one', '2'=>'two');
}
private $arrays= array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
運行此代碼時出現意外的T_VARIABLE錯誤。
我無法將函數聲明爲數組。這是我的代碼,我做錯了什麼?將函數添加到數組中php
private function array_list(){
return array('1'=>'one', '2'=>'two');
}
private $arrays= array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
運行此代碼時出現意外的T_VARIABLE錯誤。
做一個方法,例如,構造函數:
class Foo {
function __construct() {
$this->arrays['list'] = $this->array_list();
}
}
你不能像這樣的聲明數組財產:
private $arrays= array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
不能使用數組從類方法返回的屬性定義。例如,您應該將其填充到構造函數中。像這樣:
private $arrays = array();
public function __construct() {
$this->arrays = array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
}
謝謝!效果很好 – user389767 2012-01-05 01:15:40
請提供全班。 – davogotland 2012-01-05 00:47:37
http://stackoverflow.com/questions/1499862/can-you-store-a-function-in-a-php-array – c69 2012-01-05 00:47:41
在定義類的屬性時,不能使用變量。在屬性定義中提供的所有內容都必須是(不變的,而不是動態的)。 – Kenaniah 2012-01-05 01:00:59