Summary of hook function in PHP

Keywords: Programming

Hook explanation


Hook definition


Hook is a common concept in programming, which is very important. It makes the system very easy to expand (without understanding its internal implementation mechanism, which can reduce a lot of work).
It can be understood that when a glass ball falls from the air and is about to hit a person, an event will happen in advance. For example, tell the person who is hit that the ball is already in the process of falling,
Telling is an event, a hook. We can make different responses for different people. If it's a man, we can tell him that it doesn't hurt when he hits the ball. If it's a woman, we can tell her that it hurts

Crochet action

Hook functions can intercept and process messages from other applications. Whenever a specific message is sent, the hook program captures the message before it reaches the destination window, that is, the hook function gets control first. At this time, the hook function can process (change) the message, continue to deliver the message without processing, and force the end of message delivery.


Hook implementation

/*The complete implementation of hooks should be called event driven. Event driven is divided into two stages. The first stage is to register events, aiming to give a name to the "events" that may occur in the future. The simple implementation method is
Use the singleton mode to generate a persistent object or register a global variable, and then insert the event name and the corresponding class and method of the event into the global variable. That is to mount a hook.


The second stage is to trigger events. In essence, it is to query the event name to trigger in the global variables of the event, find the registered classes and methods, instantiate and run them. That way, you can put it
In order to get rid of the traditional way, the program must follow the rules in order to further realize the purpose of decoupling.

*/

Code example 1

  1. class Ball{  
  2.       
  3.     public function down(){  
  4.         echo    "ball is downing ";  
  5.         //Register events  
  6.         Hook::add("man");  
  7.         Hook::add("Women");  
  8.     }  
  9.       
  10.     public function do(){  
  11.         Hook::exec();  
  12.     }     
  13.   
  14. }  
  15.   
  16. //Definition of hook  
  17. class Hook{  
  18.     private $hooklist = null ;  
  19.     //Add  
  20.     public function add($people){         
  21.         $this->hooklist[] =  new $people();        
  22.     }  
  23.     //Trigger event  
  24.     public function exec(){  
  25.         foreach($this->hooklist as $people){  
  26.               $addon ->act();  
  27.         }  
  28.           
  29.     }  
  30. }  
  31. //Hook implementation  
  32. class man(){  
  33.     public function act(){  
  34.         echo 'notiong';  
  35.     }     
  36. }  
  37.   
  38. class WoMan(){  
  39.     public function act(){  
  40.         echo 'oh my god ';  
  41.     }     
  42. }  
  43.   
  44. class child(){  
  45.     public function act(){  
  46.         echo 'oh my god ';  
  47.     }     
  48. }  
  49.   
  50.   
  51. $ball = new Ball();  
  52. $ball ->down();  
  53. $ball ->do();  

Code example 2

  1. //If you need to add a child, you can add a child Hook::add("child");  
  2.   
  3.  /*=========================Upgraded version of hook============================================/*  
  4.   
  5.   
  6. class Hook{  
  7.       
  8.       private  $hookList;  
  9.         
  10.       //Add to  
  11.     function add($name,$fun){  
  12.         $this->hookList[$name][] = $fun;  
  13.  }  
  14.    
  15. function excec($name){  
  16.     $value = func_get_args();  
  17.     unset($value[0]);  
  18.     foreach ($this->hookList[$nameas $key => $fun) {  
  19.         call_user_func_array($fun$value);  
  20.     }  
  21. }  
  22.    
  23.       
  24.       
  25. }  
  26.   
  27.   
  28. $hook = new Hook();  
  29. $hook->add('women',function($msg){  
  30.     echo 'oh my god'.$msg ;   
  31. })  
  32.   
  33. $hook->add('man',function($msg){  
  34.     echo 'nothing'.$msg ;   
  35. })  
  36.   
  37.   
  38. //Execution  
  39. $hook->excec('man','taoge');  
  40. $hook->excec('women','xxx');  




Posted by Rusnoff on Mon, 04 May 2020 07:00:59 -0700