How to implement hook mechanism in PHP

Source: Internet
Author: User
Tags php framework
This article mainly introduces how to implement hook mechanism in PHP, introduces the principle and realization process of hook mechanism in detail, has certain reference value, interested can understand

The concept of "hook" is not really familiar with, recently saw a PHP framework used in this mechanism to extend the project, so probably to understand the next.

The so-called hook mechanism is a popular technology from Windows programming. The main idea is to bury (preset) a hook in advance where it might add functionality, and this hook has no practical meaning, and when we need to re-modify or add the logic of this place, we attach the extended class or method to this point.

The basic idea of hook plug-in mechanism:

In the project code, you think that you want to extend (temporarily not extend) place a hook function, and so on need to extend the time, the need to implement the classes and functions attached to this hook, you can implement the extension.

The idea is that it sounds more general and looks at an example of an online implementation.

The entire plug-in mechanism consists of three parts:

1.hook Plugin Manager class: This is the core file, which is an application global object. It has three main responsibilities

1> listens for all the plugins that have been registered and instantiates the plug-in objects.

2> Register all plugins.

3> triggers the corresponding object method when the hook condition is met.

2. Plug-in functionality implementation: This is mostly done by third-party developers, but it is necessary to follow our (Manager class definition) rules, which are defined by the plug-in mechanism, and vary depending on the plug-in mechanism.

3. Plugin trigger: That is, the trigger condition of the hook. This is a small piece of code that is placed where you need to call the plugin to trigger the hook.

----------------------------------take a look at the solutions that others have achieved--------------------------------

The first is the Plugin manager class PlugInManager, this class to be placed in the global reference, in all need to use the plug-in place, priority loading.

<?php/**** plug-in mechanism implementation core Class */class pluginmanager{/** * Listen to Registered plugins * * @access private * @var array */Private $_li   Steners = Array (); /** * Constructor * * @access public * @return void */Public function __construct () {#这里 $plugin array contains the plug-in that we obtain that has been activated by the user    Piece of information #为演示方便, we assume that $plugin contains at least # $plugin = Array (# ' name ' = ' Add ' plugin name ', # ' directory ' + ' plugin install directory ' #); $plugins = Get_active_plugins (); #这个函数请自行实现 if ($plugins) {foreach ($plugins as $plugin) {//assume that each plug-in folder contains an AC tions.php file, which is the specific implementation of the plug-in if (@file_exists (Stpath. plugins/'. $plugin [' directory ']. ' /actions.php ') {include_once (Stpath. plugins/'. $plugin [' directory ']. '          /actions.php '); $class = $plugin [' name ']. '          _actions ';          if (class_exists ($class)) {//Initialize all plug-in new $class ($this); }}}} #此处做些日志记录方面的东西}/** * Register the plug-in method to be monitored (hooks) * * @param string $hook * @param object $refere NCE * @param string $method  */function Register ($hook, & $reference, $method) {//Get plug-ins to implement the method $key = Get_class ($reference). '    -$method;    Push the plug-in reference together with the method into the listening array $this->_listeners[$hook] [$key] = Array (& $reference, $method);  #此处做些日志记录方面的东西}/** * triggers a hook * * @param string $hook The name of the hook * @param mixed $data The entry of the hook * @return Mixed * *    Function trigger ($hook, $data = ') {$result = '; See if the hooks to implement are in the listening array if (Isset ($this->_listeners[$hook]) && Is_array ($this->_listeners[$hook]) && Amp      Count ($this->_listeners[$hook]) > 0) {//Loop call start foreach ($this->_listeners[$hook] as $listener)        {//Remove references and methods for plug-in objects $class =& $listener [0];        $method = $listener [1];        if (Method_exists ($class, $method)) {//dynamically call the plug-in method $result. = $class $method ($data);  }}} #此处做些日志记录方面的东西 return $result; }}

Next is the implementation of a simple plug-in demo_actions. This is a simple Hello World plugin that is used to output a word. In practice, Say_hello may include operations on the database, or some other specific logic.

<?php/*** This is an implementation of the Hello World simple plugin *//*** Several default rules to note: *  1. The file name of this plug-in class must be action* 2. The name of the plug-in  class must be {plug-in name _actions}*/ The parameter of class demo_actions{  //Parse function is PlugInManager reference  function __construct (& $pluginManager)  {    // Register this plugin    //The first parameter is the name of the hook//The    second parameter is a reference to PlugInManager    //The third is the method that the plugin executes    $pluginManager->register (' Demo ', $this, ' Say_hello ');  }  function Say_hello ()  {    echo ' Hello World ';  }}

Next is the plug-in call trigger place, for example I want to put Say_hello on my blog home index.php, then you in index.php somewhere in a place to write:

$pluginManager->trigger (' demo ', ');

The first parameter represents the name of the hook, and the second parameter is the entry parameter of the plug-in corresponding method, which is empty because there is no input parameter in this example.

Such an example is basically a clear expression of the "hook" plug-in mechanism implementation and logic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.