說我有一個私人調度表的類。如何在PHP中的類中創建調度表?
$this->dispatch = array(
1 => $this->someFunction,
2 => $this->anotherFunction
);
如果我再調用
$this->dispatch[1]();
我得到一個錯誤,該方法是不是一個字符串。當我做一個字符串是這樣的:
$this->dispatch = array(
1 => '$this->someFunction'
);
這將產生 致命錯誤:調用未定義的函數$這個 - > someFunction()
我一直在使用也嘗試:
call_user_func(array(SomeClass,$this->dispatch[1]));
產生消息:call_user_func(SomeClass :: $ this-> someFunction)[function.call-user-func]:第一個參數預計是一個有效的回調。
編輯:我意識到這並沒有什麼意義,因爲當$ this是SomeClass時它調用SomeClass :: $ this。我已經嘗試了這幾種方式,與陣列包含
array($this, $disptach[1])
這仍然不能完成我所需要的。
編輯完
如果我沒有一類,只是有一些功能的調度文件,這工作。例如,這個工程:
$dispatch = array(
1 => someFunction,
2 => anotherFunction
);
我不知道是否有一種方法,我仍然可以保留這些在類的私有方法,但仍與調度表使用它們。
對兩種方法進行基準測試並不難。 – 2008-11-21 18:57:26