Home > Article > Backend Development > Understanding and practical tutorials on hooks in PHP
Hook definition
Hook is a common concept in programming and is very important. It makes the system very easy to expand (without having to understand its internal implementation mechanism, which can reduce a lot of workload).
It can be understood that when a glass ball falls from the sky and is about to hit someone, an event will happen in advance. For example, tell the person who was hit that the ball is already falling. Telling is an event, a hook. We can make different responses to different people. If it is a man, we tell him whether the ball hit someone or not. It hurts. If it’s a woman, tell her it hurts.
The role of hook
The hook function 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 first obtains control. At this time, the hook function can process (change) the message, continue to deliver the message without processing it, or force the message delivery to end.Hook implementation
The complete implementation of hook should be called event driven. Event driving is divided into two stages. The first stage is to register events, with the purpose of giving a name to "events" that may occur in the future.The simple implementation method is:
Use singleton mode to generate a persistent object or register a global variable, and then add the event name and the class corresponding to the event Just insert global variables with the method. 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, we can get rid of the traditional rules that programs must be in order, and further achieve the purpose of decoupling.Code example 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 example 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:
The above is the detailed content of Understanding and practical tutorials on hooks in PHP. For more information, please follow other related articles on the PHP Chinese website!