Home >Backend Development >PHP Problem >What does PHP hook mean?

What does PHP hook mean?

Guanhui
GuanhuiOriginal
2020-07-31 14:21:585192browse

Hook is an event-driven message processing mechanism that can intercept and process messages from other applications. Whenever a specific message is sent, the hook program will capture the message before it reaches the destination window. That is, the hook function gets control first.

What does PHP hook mean?

Hook implementation

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

The second stage is to trigger the event, which is essentially to query the event global variable for the name of the event to be triggered, then find the registered class and method, instantiate and run. In this way, you can get rid of the traditional rules that programs must be in order, and further achieve the purpose of decoupling.

Code sample one

class Ball{
public function down(){
echo    "ball is downing ";
//注册事件
Hook::add("man");
Hook::add("Women");
}
public function do(){
Hook::exec();
}
 
}
 
// 钩子的定义
class Hook{
private $hooklist = null ;
// 添加
public function add($people){
$this->hooklist[] =  new $people();
}
// 触发事件
public function exec(){
foreach($this->hooklist as $people){
  $addon ->act();
}
}
}
// 钩子实现
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 two

// 如果需要添加小孩,就可以做添加一个小孩Hook::add("child");
 
 /*=========================钩子的升级版============================================/*
class Hook{
  private  $hookList;
  
  //添加
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 ; 
})
// 执行
$hook->excec('man','taoge');
$hook->excec('women','xxx');

Recommended tutorial: "PHP"

The above is the detailed content of What does PHP hook mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn