fork download
  1. <?php
  2.  
  3. class Phone
  4. {
  5. public function addnewnumber($name, $number)
  6. {
  7. echo "Phone::addnnewnumber\n";
  8. }
  9. }
  10.  
  11. class Proxy
  12. {
  13. private $_target;
  14. public function __construct($target)
  15. {
  16. $this->_target = $target;
  17. }
  18.  
  19. public function __call($name, $params)
  20. {
  21. $callable = array($this->_target, $name);
  22. if (!is_callable($callable)) {
  23. trigger_error('Call to undefined method '.
  24. get_class($this->_target).'::'.$name, E_USER_ERROR);
  25. }
  26.  
  27. return $this->dispatch($callable, $params);
  28. }
  29.  
  30. protected function dispatch($callable, $params)
  31. {
  32. return call_user_func_array($callable, $params);
  33. }
  34. }
  35.  
  36. class LoggingProxy extends Proxy
  37. {
  38. protected function dispatch($callable, $params)
  39. {
  40. echo "Before calling ".get_class($callable[0]).'::'.$callable[1]."\n";
  41. $return = parent::dispatch($callable, $params);
  42. echo "After calling ".get_class($callable[0]).'::'.$callable[1]."\n";
  43. return $return;
  44. }
  45. }
  46.  
  47. $a = new LoggingProxy(new Phone);
  48. $a->addnewnumber(1, 2);
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
Before calling Phone::addnewnumber
Phone::addnnewnumber
After calling Phone::addnewnumber