fork(1) download
  1. <?php
  2.  
  3. class car {
  4.  
  5. //method to get class method
  6. public function get_method($method_name) {
  7. $class = new ReflectionClass(get_class($this));
  8. $method = $class->getMethod($method_name);
  9. $method->setAccessible(true);
  10. return $method;
  11. }
  12.  
  13. public function exec_method($method_name, $arg_args=array()) {
  14.  
  15. //execute the pre() function before the specified method
  16. $this->pre();
  17.  
  18. //execute the specified method
  19. $this->get_method($method_name)->invokeArgs($this, $arg_args);
  20. }
  21.  
  22. public function pre() {
  23. echo 'pre';
  24. echo '<br />';
  25. }
  26. }
  27.  
  28. class toyota extends car {
  29. private function drive() {
  30. echo 'drive';
  31. echo '<br />';
  32. }
  33.  
  34. private function brake() {
  35. echo 'brake';
  36. echo '<br />';
  37. }
  38. }
  39.  
  40. $toyota = new toyota();
  41. $toyota->exec_method('drive');
  42. $toyota->exec_method('brake');
  43. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
pre<br />drive<br />pre<br />brake<br />