
What is a hook?
You must have heard of plug-ins. There are many WordPress plug-ins. This is implemented using the hook mechanism.
When the code is running, we execute some special methods in advance at several special points of the operation: for example, recording input parameters and running methods before running methods (such as the add method of Blog::add) After that, the processing results are recorded. Before and after the running method, there is a simple hook (mount point). We place the hook function on this hook (record the input parameters and record the processing results) to perform some tasks that are not related to the running of the program. .
<?php
class Blog extends Controller{
public function add(){
//some code
$res = $data;
return $res;
}
}
$obj = new Blog();
Log::write($_REQUEST);
$res = $obj->add();
Log::write(json_encode($res));If an OnBeforeRunActionCallback() method is placed before running the method, this method may be empty at the beginning, but we can directly use OnBeforeRunActionCallback() without modifying the original code in the future. Just add code logic inside, such as logging, parameter filtering, etc.
<?php
class Blog extends Controller{
public function add(){
//some code
$res = $data;
return $res;
}
}
$obj = new Blog();
OnBeforeRunActionCallback($_REQUEST);
$obj->add();
OnAfterRunActionCallback($res);
function OnBeforeRunActionCallback($param){
Log::write($param);
FilterParams($param);
}
function OnAfterRunActionCallback($res){
Log::write(json_encode($res));
}In the project code, place a hook function where you think it needs to be extended (not extended yet). When it needs to be extended, mount the classes and functions that need to be implemented to this hook. The implementation has been expanded.
Principle
The actual hook is generally designed as a class Hook, which provides registration plug-ins to the hook (add_hook) and triggers the hook method (trigger_hook). When registering a plug-in, store the executable method to be run by the plug-in into the array corresponding to the hook.
$_listeners = array(
'OnBeforeRunAction' => array(
'callback1',
'callback2',
'callback3',
),
);
//提前注册插件到钩子
add_hook('OnBeforeRunAction', 'callback4');
//特定地方执行钩子
trigger_hook('OnBeforeRunAction');When the hook is triggered, the callback methods registered in OnBeforeRunAction will be traversed and the corresponding callback methods will be executed to implement dynamic expansion functions. The registered hook method is generally an anonymous function:
function trigger_hook($hook, $data=''){
//查看要实现的钩子,是否在监听数组之中
if (isset($this->_listeners[$hook]) && is_array($this->_listeners[$hook]) && count($this->_listeners[$hook]) > 0)
{
// 循环调用开始
foreach ($this->_listeners[$hook] as $listener)
{
if(is_callable()){
call_user_func($listener, $data);
}elseif(is_array($listener)){
// 取出插件对象的引用和方法
$class =& $listener[0];
$method = $listener[1];
if(method_exists($class,$method))
{
// 动态调用插件的方法
$class->$method($data);
}
}
}
}
}How to implement it?
Simple
1. Plug-in class Hook: Provides methods for registering plug-ins and executing plug-ins. It actually stores the corresponding mount points in an array. Executable methods.
2. Register plug-ins uniformly in a certain configuration file or function.
class Hook
{
//action hooks array
private static $actions = array();
/**
* ads a function to an action hook
* @param $hook
* @param $function
*/
public static function add_action($hook,$function)
{
$hook=mb_strtolower($hook,CHARSET);
// create an array of function handlers if it doesn't already exist
if(!self::exists_action($hook))
{
self::$actions[$hook] = array();
}
// append the current function to the list of function handlers
if (is_callable($function))
{
self::$actions[$hook][] = $function;
return TRUE;
}
return FALSE ;
}
/**
* executes the functions for the given hook
* @param string $hook
* @param array $params
* @return boolean true if a hook was setted
*/
public static function do_action($hook,$params=NULL)
{
$hook=mb_strtolower($hook,CHARSET);
if(isset(self::$actions[$hook]))
{
// call each function handler associated with this hook
foreach(self::$actions[$hook] as $function)
{
if (is_array($params))
{
call_user_func_array($function,$params);
}
else
{
call_user_func($function);
}
//cant return anything since we are in a loop! dude!
}
return TRUE;
}
return FALSE;
}
/**
* gets the functions for the given hook
* @param string $hook
* @return mixed
*/
public static function get_action($hook)
{
$hook=mb_strtolower($hook,CHARSET);
return (isset(self::$actions[$hook]))? self::$actions[$hook]:FALSE;
}
/**
* check exists the functions for the given hook
* @param string $hook
* @return boolean
*/
public static function exists_action($hook)
{
$hook=mb_strtolower($hook,CHARSET);
return (isset(self::$actions[$hook]))? TRUE:FALSE;
}
}
/**
* Hooks Shortcuts not in class
*/
function add_action($hook,$function)
{
return Hook::add_action($hook,$function);
}
function do_action($hook)
{
return Hook::do_action($hook);
}Usage example:
//添加钩子 Hook::add_action('unique_name_hook','some_class::hook_test'); //或使用快捷函数添加钩子: add_action('unique_name_hook','other_class::hello'); add_action('unique_name_hook','some_public_function'); //执行钩子 do_action('unique_name_hook');//也可以使用 Hook::do_action();
With installation/uninstallation
When the hook class is initialized, register the plug-in that has been opened (such as database records) ;Set the mount point when the global situation is appropriate; trigger the mount point registration event when the operation is appropriate.
1. Plug-in class Hook: Provides methods for registering plug-ins and executing plug-ins. It actually stores the executable method corresponding to the mount point in an array.
2. Add an initialization method to the plug-in class to search for installed plug-ins, run the registration method (reg) that must be executed by the plug-in, and register the plug-in method to register the hook to the mount point.
3. Always place the plug-in in a certain directory and write the configuration file according to certain specifications. There is a plug-in list page in the background, which traverses the plug-ins in the specified directory. When installing, the plug-in information is recorded in the database, and when uninstalled, the database record information is deleted.
<?php
/**
* @file plugin.php
* @brief 插件核心类
* @note 观察者模式,注册事件,触发事件
*/
class plugin extends IInterceptorBase
{
//默认开启的插件列表
private static $defaultList = array("_verification","_goodsCategoryWidget","_authorization","_userInfo","_initData");
//已经注册监听
private static $_listen = array();
//加载插件
public static function init()
{
$pluginDB = new IModel('plugin');
$pluginList = $pluginDB->query("is_open = 1","class_name","sort asc");
//加载默认插件
foreach(self::$defaultList as $val)
{
$pluginList[]= array('class_name' => $val);
}
foreach($pluginList as $key => $val)
{
$className = $val['class_name'];
$classFile = self::path().$className."/".$className.".php";
if(is_file($classFile))
{
include_once($classFile);
$pluginObj = new $className();
$pluginObj->reg();
}
}
}
/**
* @brief 注册事件
* @param string $event 事件
* @param object ro function $classObj 类实例 或者 匿名函数
* @param string $method 方法名字
*/
public static function reg($event,$classObj,$method = "")
{
if(!isset(self::$_listen[$event]))
{
self::$_listen[$event] = array();
}
self::$_listen[$event][] = array($classObj,$method);
}
/**
* @brief 显示已注册事件
* @param string $event 事件名称
* @return array
*/
public static function get($event = '')
{
if($event)
{
if( isset(self::$_listen[$event]) )
{
return self::$_listen[$event];
}
return null;
}
return self::$_listen;
}
/**
* @brief 触发事件
* @param string $event 事件
* @param mixed $data 数据
* @notice 可以调用匿名函数和方法
*/
public static function trigger($event,$data = null)
{
$result = array();
if(isset(self::$_listen[$event]))
{
foreach(self::$_listen[$event] as $key => $val)
{
list($pluginObj,$pluginMethod) = $val;
$result[$key] = is_callable($pluginObj) ? call_user_func($pluginObj,$data):call_user_func(array($pluginObj,$pluginMethod),$data);
}
}
return isset($result[1]) ? $result : current($result);
}
/**
* @brief 插件物理路径
* @return string 路径字符串
*/
public static function path()
{
return IWeb::$app->getBasePath()."plugins/";
}
/**
* @brief 插件WEB路径
* @return string 路径字符串
*/
public static function webPath()
{
return IUrl::creatUrl('')."plugins/";
}
/**
* @brief 获取全部插件
* @param string $name 插件名字,如果为空则获取全部插件信息
* @return array 插件信息 array(
"name" => 插件名字,
"description" => 插件描述,
"explain" => 使用说明,
"class_name" => 插件ID,
"is_open" => 是否开启,
"is_install" => 是否安装,
"config_name" => 默认插件参数结构,
"config_param"=> 已经保存的插件参数,
"sort" => 排序,
)
*/
public static function getItems($name = '')
{
$result = array();
$dirRes = opendir(self::path());
//遍历目录读取配置文件
$pluginDB = new IModel('plugin');
while($dir = readdir($dirRes))
{
if($dir[0] == "." || $dir[0] == "_")
{
continue;
}
if($name && $result)
{
break;
}
if($name && $dir != $name)
{
continue;
}
$pluginIndex = self::path().$dir."/".$dir.".php";
if(is_file($pluginIndex))
{
include_once($pluginIndex);
if(get_parent_class($dir) == "pluginBase")
{
$class_name = $dir;
$pluginRow = $pluginDB->getObj('class_name = "'.$class_name.'"');
$is_open = $pluginRow ? $pluginRow['is_open'] : 0;
$is_install = $pluginRow ? 1 : 0;
$sort = $pluginRow ? $pluginRow['sort'] : 99;
$config_param = array();
if($pluginRow && $pluginRow['config_param'])
{
$config_param = JSON::decode($pluginRow['config_param']);
}
$result[$dir] = array(
"name" => $class_name::name(),
"description" => $class_name::description(),
"explain" => $class_name::explain(),
"class_name" => $class_name,
"is_open" => $is_open,
"is_install" => $is_install,
"config_name" => $class_name::configName(),
"config_param"=> $config_param,
"sort" => $sort,
);
}
}
}
if(!$name)
{
return $result;
}
return isset($result[$name]) ? $result[$name] : array();
}
/**
* @brief 系统内置的所有事件触发
*/
public static function onCreateApp(){plugin::init();plugin::trigger("onCreateApp");}
public static function onFinishApp(){plugin::trigger("onFinishApp");}
public static function onBeforeCreateController($ctrlId){plugin::trigger("onBeforeCreateController",$ctrlId);plugin::trigger("onBeforeCreateController@".$ctrlId);}
public static function onCreateController($ctrlObj){plugin::trigger("onCreateController");plugin::trigger("onCreateController@".$ctrlObj->getId());}
public static function onFinishController($ctrlObj){plugin::trigger("onFinishController");plugin::trigger("onFinishController@".$ctrlObj->getId());}
public static function onBeforeCreateAction($ctrlObj,$actionId){plugin::trigger("onBeforeCreateAction",$actionId);plugin::trigger("onBeforeCreateAction@".$ctrlObj->getId());plugin::trigger("onBeforeCreateAction@".$ctrlObj->getId()."@".$actionId);}
public static function onCreateAction($ctrlObj,$actionObj){plugin::trigger("onCreateAction");plugin::trigger("onCreateAction@".$ctrlObj->getId());plugin::trigger("onCreateAction@".$ctrlObj->getId()."@".$actionObj->getId());}
public static function onFinishAction($ctrlObj,$actionObj){plugin::trigger("onFinishAction");plugin::trigger("onFinishAction@".$ctrlObj->getId());plugin::trigger("onFinishAction@".$ctrlObj->getId()."@".$actionObj->getId());}
public static function onCreateView($ctrlObj,$actionObj){plugin::trigger("onCreateView");plugin::trigger("onCreateView@".$ctrlObj->getId());plugin::trigger("onCreateView@".$ctrlObj->getId()."@".$actionObj->getId());}
public static function onFinishView($ctrlObj,$actionObj){plugin::trigger("onFinishView");plugin::trigger("onFinishView@".$ctrlObj->getId());plugin::trigger("onFinishView@".$ctrlObj->getId()."@".$actionObj->getId());}
public static function onPhpShutDown(){plugin::trigger("onPhpShutDown");}
}
/**
* @brief 插件基类,所有插件必须继承此类
* @notice 必须实现3个抽象方法: reg(),name(),description()
*/
abstract class pluginBase extends IInterceptorBase
{
//错误信息
protected $error = array();
//注册事件接口,内部通过调用payment::reg(事件,对象实例,方法);
public function reg(){}
/**
* @brief 默认插件参数信息,写入到plugin表config_param字段
* @return array("字段名" => array(
"name" => "文字显示",
"type" => "数据类型【text,radio,checkbox,select】",
"pattern" => "数据校验【int,float,date,datetime,require,正则表达式】",
"value" => "1,数组:枚举数据【radio,checkbox,select】的预设值,array(名字=>数据); 2,字符串:【text】默认数据",
))
*/
public static function configName()
{
return array();
}
/**
* @brief 插件安装
* @return boolean
*/
public static function install()
{
return true;
}
/**
* @brief 插件卸载
* @return boolean
*/
public static function uninstall()
{
return true;
}
/**
* @brief 插件名字
* @return string
*/
public static function name()
{
return "插件名称";
}
/**
* @brief 插件功能描述
* @return string
*/
public static function description()
{
return "插件描述";
}
/**
* @brief 插件使用说明
* @return string
*/
public static function explain()
{
return "";
}
/**
* @brief 获取DB中录入的配置参数
* @return array
*/
public function config()
{
$className= get_class($this);
$pluginDB = new IModel('plugin');
$dataRow = $pluginDB->getObj('class_name = "'.$className.'"');
if($dataRow && $dataRow['config_param'])
{
return JSON::decode($dataRow['config_param']);
}
return array();
}
/**
* @brief 返回错误信息
* @return array
*/
public function getError()
{
return $this->error ? join("\r\n",$this->error) : "";
}
/**
* @brief 写入错误信息
* @return array
*/
public function setError($error)
{
$this->error[] = $error;
}
/**
* @brief 插件视图渲染有布局
* @param string $view 视图名字
* @param array $data 视图里面的数据
*/
public function redirect($view,$data = array())
{
if($data === true)
{
$this->controller()->redirect($view);
}
else
{
$__className = get_class($this);
$__pluginViewPath = plugin::path().$__className."/".$view;
$result = self::controller()->render($__pluginViewPath,$data);
if($result === false)
{
IError::show($__className."/".$view."插件视图不存在");
}
}
}
/**
* @brief 插件视图渲染去掉布局
* @param string $view 视图名字
* @param array $data 视图里面的数据
*/
public function view($view,$data = array())
{
self::controller()->layout = "";
$this->redirect($view,$data);
}
/**
* @brief 插件物理目录
* @param string 插件路径地址
*/
public function path()
{
return plugin::path().get_class($this)."/";
}
/**
* @brief 插件WEB目录
* @param string 插件路径地址
*/
public function webPath()
{
return plugin::webPath().get_class($this)."/";
}
}For more related php knowledge, please visit php tutorial!
The above is the detailed content of PHP hook mechanism principle and detailed explanation. For more information, please follow other related articles on the PHP Chinese website!
PHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AMPHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.
PHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AMPHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.
Why Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AMThe core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.
Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AMPHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.
The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AMPHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.
PHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AMPHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
PHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AMPHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AMUsing preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft






