本質上來說,我正在尋找一種通用的「漏斗」方式來進行自動記錄。這是簡短的描述,實際上我們假設我們有一個類控制器,現在很像在codeIgniter中,一切都通過Controller運行,但是我想創建一些將所有請求都彙集到Controller的通過這個類來進行通用日誌記錄。下面是一個例子...在魔術方法中分解參數__call php
class Base {
protected $_controller;
public function __construct() {
$this->_controller = new Controller();
}
public function __get($key) {
$this->_logger->log('you are getting '.$key);
return $this->_controller->$key;
}
// and so on for all the magic methods, __set, __get, __call, __callStatic
}
這裏的問題是__call方法,因爲它使ARGS一個數組,如果我必須要通過2個參數傳遞給控制器它破壞了一切,即
public function __call($method, $args) {
//obviously call to logging and make sure method_exists here
return $this->_controller->$method($args);
}
然而,如果有什麼方法需要兩個參數是這樣的...
//this would be inside the Controller
public function get_stats($start_date, $end_date) {
//blah blah lots of code here
}
,如果我當時稱爲BASE-> get_stats(「2011-01-01」,「2013年10月19日」),一切都將打破,因爲只有1 arg被傳遞給Controller方法,因爲__call將所有參數加入到一個數組中。顯然,如果我知道總會有2個參數,那麼我只會得到$ args [0]和$ args [1],但是這裏的理論是將它作爲真正的動態函數,以便所有的函數調用都通過Base類進行傳遞,控制器中的函數可能有100萬個參數。 有沒有人有任何想法? 我試圖call_user_func_array,但它試圖調用從一個類的所有方法以靜態方式,即
//inside base class
public function __call($method, $args) {
//check for function and all that stuff here
return call_user_func_array(array($this->_controller, $method), $args);
}
將拋出一個錯誤,因爲在控制器的方法是非靜態的。我很茫然,但我真的想做這個工作,所以有什麼想法?謝謝,麻煩您了。
「我曾嘗試call_user_func_array,但它試圖以靜態的方式調用一個類的所有方法「 - 不是嗎?它不應該,我想?你在想'__callStatic'嗎?它出現了什麼錯誤? – h2ooooooo