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.
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 oneclass 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
