PHP Plugin Technology-plug-in hook (hooks) analysis

Source: Internet
Author: User
Recently prepared to do an open-source personal blog system, because in the idea of adding plug-in functionality, so the plug-in function to study the implementation method.

Plug-in function according to my own understanding is the existing program to add and improve the function, plug-in to the interface provided by the program to connect, and then through the interface has been connected to the program's data and so on to modify.

and the role of plug-in hook is similar to a plug-in interface, plug-in and the corresponding hooks in the program to connect, the program provides content to modify.

My plug-in mechanism is all based on the object-oriented approach to the schema, in the beginning to initialize the plug-in, is to use an array to store the name of the plug-in for subsequent hooks to invoke.

$plug _config[]=array (    ' plug_name ' = ' oneplug ',    ' plug_version ' = ' 1.01 ');

This plugin function I just want to add a nice look to a piece of HTML content, plugins I put in the./root/ext folder, with the plug-in name as the name of the plug-in folder, main.php as the main invocation portal of the plug-in.

Inside the plugin is a class with the name of the plugin.

 
  ', $plug _tem_global);                $plug _tem_global= '. Implode ("

", $temarr).";}

This global $plug _tem_global; is to use a global variable $plug_tem_global as the parameter of the incoming content, modify it to be equivalent to modify the content.

In the plug-in mechanism of the main file, plug-in hook as a call plug-in interface, it is actually a string variable, its internal value is the name of the array to insert the current location plug-in

For example, the plug-in initialization of the array name is Plug_config, then the plug-in hook here is $plug_config, because of the inside of the class to manipulate the variable, so use global to turn him into a global variable.

$plug _point_name= ' Plug_config '; global $ $plug _point_name;

For plug-in execution, a loop is used internally to execute all plugins that apply to the current hook

$plug _tem_arr=$ $plug _point_name $plug _num=count ($plug _tem_arr); for ($plug _i=0; $plug _i< $plug _num; $plug _i++) {    if (isset ($plug _tem_arr[$plug _i][' plug_name ')) {        $plug _name= $plug _tem_arr[$plug _i][' plug_name '];        $plug _file_address=root. ' ext '. Directory_separator. $plug _name. Directory_separator. ' Main.php ';        if (file_exists ($plug _file_address)) {            require ($plug _file_address);            $plug _tem_obj=new $plug _name ();}}    

Below is I do not use plug-ins and use the effect of the plug-in comparison, haha ~ Once there is a mountain ...

  • 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.