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 isUse 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
- class Ball{
- public function down(){
- echo "ball is downing ";
- //Register events
- 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 example 2
- //If you need to add a child, you can add a child Hook::add("child");
- /*=========================Upgraded version of hook============================================/*
- class Hook{
- private $hookList;
- //Add to
- 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 ;
- })
- //Execution
- $hook->excec('man','taoge');
- $hook->excec('women','xxx');