基本的に、自動ロギング用の普遍的な「じょうご」のようなものを探しています。これが簡単な説明です。実際には、クラス Controller があるとしましょう。codeIgniter のように、ほとんどすべてが Controller を介して実行されますが、ユニバーサル ロギングのために、このクラスを介して Controller へのすべてのリクエストをファネルする何かを作成したいと考えています。ここに例があります...
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
}
ここでの問題は、args を配列にする __call メソッドです。コントローラーに 2 つの引数を渡す必要がある場合、すべてが台無しになります。
public function __call($method, $args) {
//obviously call to logging and make sure method_exists here
return $this->_controller->$method($args);
}
ただし、メソッドがこのような 2 つの引数を必要とする場合はどうなるでしょうか...
//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') を呼び出すと、__call がすべての引数を 1 つの配列に結合する方法のために、1 つの引数のみが Controller メソッドに渡されるため、すべてが壊れます。明らかに、常に 2 つの引数が存在することがわかっている場合は、$args[0] と $args[1] を取得するだけですが、ここでの理論は、これを真に動的なものにして、すべての関数呼び出しが Base クラスを通過し、 Controller の関数は、100 万から 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);
}
Controller のメソッドは静的ではないため、エラーがスローされます。私は途方に暮れていますが、私は本当にこれを機能させたいので、何かアイデアはありますか? よろしくお願いします。