Talking about how to implement the Hook mechanism in PHP, talking about the phphook Mechanism

Source: Internet
Author: User

Talking about how to implement the Hook mechanism in PHP, talking about the phphook Mechanism

I am not familiar with the concept of "Hook". Recently I saw a php framework using this mechanism to expand projects. So I 'd like to know about it.

The so-called Hook mechanism is a popular technology in Windows programming. The main idea is to bury (preset) a hook in advance where functions may be added. This hook has no practical significance, when we need to re-modify or add the logic of this place, mount the extended class or method to this point.

The basic idea of hook plug-in mechanism:

In the project code, place a hook function in the place where you think you want to expand (temporarily not extended), and mount the classes and functions to be implemented to this hook when you need to expand, you can implement the extension.

The idea is that it sounds General. Let's look at an online implementation example.

The entire plug-in mechanism consists of three parts:

1. hook plug-in manager class: this is the core file and is a Global object of the application. It has three main responsibilities:

1> listen to all registered plug-ins and instantiate these plug-ins.

2> register all plug-ins.

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

2. plug-in function implementation: This is mostly done by third-party developers, but we need to follow the rules defined by the manager class. This rule is defined by the plug-in mechanism, depending on the plug-in mechanism.

3. Plug-In trigger: the trigger condition of the hook. This is a short piece of code, which is placed where you need to call the plug-in to trigger this hook.

---------------------------------- Let's take a look at the solutions implemented by others --------------------------------

The first is the plug-in manager class PluginManager. This class should be placed in the global reference. In all the places where plug-ins are needed, the class should be loaded first.

<? Php/*** plug-in mechanism implementation core class */class PluginManager {/*** listen to registered plug-ins ** @ access private * @ var array */private $ _ listeners = array (); /*** constructor *** @ access public * @ return void */public function _ construct () {# Here, the $ plugin array contains the information about the plug-ins that have been activated by users # For ease of demonstration, assume that $ plugin contains at least # $ plugin = array (# 'name' => 'plug-In name', # 'Directory '=> 'plug-in installation directory '#); $ plugins = get_active_plugins (); # implement the if ($ plugins) {foreach ($ plugins as $ plugin) {// assume that each plug-in folder contains an actions. PHP file, which is the specific implementation of the plug-in if (@ file_exists (STPATH. 'ins INS /'. $ plugin ['directory']. '/actions. php ') {include_once (STPATH. 'ins INS /'. $ plugin ['directory']. '/actions. php '); $ class = $ plugin ['name']. '_ actions'; if (class_exists ($ class) {// Initialize all plug-ins new $ class ($ this );}}}} # Here are some log record items}/*** register the plug-in method (Hook) to be monitored) ** @ param string $ hook * @ param object $ reference * @ param string $ method */function register ($ hook, & $ reference, $ method) {// obtain the method to be implemented by the plug-in $ key = get_class ($ reference ). '-> '. $ method; // push the reference of the plug-in along with the method into the listening array $ this-> _ listeners [$ hook] [$ key] = array (& $ reference, $ method ); # Here are some log records}/*** trigger a hook ** @ param string $ hook name * @ param mixed $ data hook input parameter * @ return mixed */function trigger ($ hook, $ data = '') {$ result =''; // view the hook to be implemented, in the listener array, if (isset ($ this-> _ listeners [$ hook]) & is_array ($ this-> _ listeners [$ hook]) & count ($ this-> _ listeners [$ hook])> 0) {// start the foreach loop call ($ this-> _ listeners [$ hook] as $ listener) {// retrieve the reference and method of the plug-in object $ class = & $ listener [0]; $ method = $ listener [1]; if (method_exists ($ class, $ method )) {// dynamically call the ins method $ result. = $ class-> $ method ($ data) ;}}# return $ result for some logging items here ;}}

The following is a simple plug-in implementation of DEMO_actions. This is a simple Hello World plug-in used to output a sentence. In practice, say_hello may include database operations or other specific logic.

<? Php/*** this is the implementation of a simple Hello World plug-in * // Several default rules that need to be paid attention to: * 1. the file name of this plug-in class must be action * 2. the plug-in class name must be {plug-in name _ actions} */class DEMO_actions {// The resolution function parameter is the reference function _ construct (& $ pluginManager) of pluginManager) {// register this plug-in // The first parameter is the hook name // The second parameter is the reference of pluginManager // The third parameter is the method executed by the plug-in $ pluginManager-> register ('Demo ', $ this, 'Say _ hello');} function say_hello () {echo 'Hello world ';}}

The next step is where the plug-in call is triggered. For example, if you want to put say_hello to Index. php on the homepage of my blog, write it at a location in index. php:

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

The first parameter indicates the hook name, and the second parameter is the entry parameter of the method corresponding to the plug-in. It is null because no parameter is input in this example.

This example basically clearly expresses the implementation method and logic of the "Hook" plug-in mechanism.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.