Hooks in PHP (Hook plugin mechanism)

Source: Internet
Author: User
Tags php framework

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 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 of the core class*/classpluginmanager{/** * Listen for registered plugins * * @access private * @var array*/    Private $_listeners=Array(); /** * Constructor * * @access public * @return void*/     Public function__construct () {#here the $plugin array contains information about the plugins that we have already activated by the user     #for demonstration purposes, we assume that the $plugin contains at least     #$plugin = Array (        #' name ' = ' plugin ',        #' directory ' = ' plugin installation directory '        #);        $plugins= Get_active_plugins ();#This function should be implemented by itself        if($plugins)        {            foreach($plugins  as $plugin)            {//assume that each plug-in folder contains a actions.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 plugins                        New $class($this); }                }            }        }        #do some logging here .    }    /** * Register the plug-in method (hook) to be monitored * * @param string $hook * @param object $reference * @param string $method */    functionRegister$hook, &$reference,$method)    {        //get the plug-in method to implement        $key=Get_class($reference).‘ '.$method; //Push the plugin's reference together with the method into the listener array        $this->_listeners[$hook][$key] =Array(&$reference,$method); #do some logging here .    }    /** * trigger a hook * * @param string $hook The name of the hook * @param mixed $data Hook's entry * @return mixed*/    functionTrigger$hook,$data= ' ')    {        $result= ' '; //See if the hooks to implement are in the listener array        if(isset($this->_listeners[$hook]) &&Is_array($this->_listeners[$hook]) &&Count($this->_listeners[$hook]) > 0)        {            //Loop Call Start            foreach($this->_listeners[$hook] as $listener)            {                //Remove references and methods from plug-in objects                $class=&$listener[0]; $method=$listener[1]; if(method_exists($class,$method))                {                    //methods for dynamically invoking plug-ins                    $result.=$class-$method($data); }            }        }        #do some logging here .        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}*/classdemo_actions{//The argument of the analytic function is a reference to the PlugInManager    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 '); }    functionSay_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.

Link

Hooks in PHP (Hook plugin mechanism)

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.