Understanding of Hook in php and Analysis of Application Example

  • 2021-12-19 06:14:30
  • OfStack

This paper describes the understanding and application of hooks in php with examples. Share it for your reference, as follows:

Interpretation of hook

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 workload).
It can be understood that when a glass ball falls from the air and is about to hit a person, an event will happen ahead of time. For example, telling the smashed person that the ball is already falling.
Telling is an event and a hook. We can make different responses to different people. If it is a man, we tell him that the ball does not hurt when it hits people. If it is a woman, we tell her that it hurts;

Hook action

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

Hook implementation

/* The complete implementation of the hook should be called event-driven. Event-driven is divided into two stages. The first stage is to register events, which aims to give a name to "events" that may occur in the future. The simple implementation method is as follows
Use singleton pattern to generate a persistent object or register a global variable, and then insert the event name, as well as the corresponding class and method of the event into the global variable. That is, mount a hook.
The second stage is to trigger the event, which is essentially to query the name of the event to be triggered in the global variable of the event, then find the registered class and method, instantiate and run it. You can put it in this way
Break away from the rule that programs must be sequenced in the traditional way, and take one step to achieve the purpose of decoupling.
*/

Code Sample 1


class Ball{
 public function down(){
 echo  "ball is downing ";
 // Register event 
 Hook::add("man");
 Hook::add("Women");
 }
 public function do(){
 Hook::exec();
 }
}
//  Definition of hook 
class Hook{
 private $hooklist = null ;
 //  Add 
 public function add($people){
 $this->hooklist[] = new $people();
 }
 //  Trigger event 
 public function exec(){
 foreach($this->hooklist as $people){
   $addon ->act();
 }
 }
}
//  Hook implementation 
class man(){
 public function act(){
 echo 'notiong';
 }
}
class WoMan(){
 public function act(){
 echo 'oh my god ';
 }
}
class child(){
 public function act(){
 echo 'oh my god ';
 }
}
$ball = new Ball();
$ball ->down();
$ball ->do();

Code Sample 2


//  If you need to add children, , You can add it 1 A child Hook::add("child");
 /*========================= An upgraded version of the hook ============================================/*
class Hook{
  private $hookList;
  // Add 
 function add($name,$fun){
 $this->hookList[$name][] = $fun;
 }
function excec($name){
  $value = func_get_args();
  unset($value[0]);
  foreach ($this->hookList[$name] as $key => $fun) {
    call_user_func_array($fun, $value);
  }
}
}
$hook = new Hook();
$hook->add('women',function($msg){
 echo 'oh my god'.$msg ;
})
$hook->add('man',function($msg){
 echo 'nothing'.$msg ;
})
//  Execute 
$hook->excec('man','taoge');
$hook->excec('women','xxx');

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: