PHP plug-in technology-plug-in Hook (hooks) analysis recently prepared for an open-source personal blog system, because the plug-in function needs to be added in the idea, so we have studied the implementation of the plug-in function.
According to my own understanding, the function of the plug-in is to add and improve the functions of existing programs. The plug-in should be connected to the interfaces provided by the program, then, modify the program data through the connected interface.
The plug-in hook function is similar to a plug-in interface. The Plug-In connects to the corresponding hook in the program and modifies the content provided by the program.
All of my plug-in mechanisms adopt an object-oriented architecture. at the beginning, you need to initialize the plug-in by using an array to store the plug-in name, for subsequent hook calls.
$plug_config[]=array( 'plug_name'=>'onePlug', 'plug_version'=>'1.01');
I just want to add a nice style to the html content of this plug-in. in the/root/ext folder, use the plug-in name as the name of the plug-in folder, main. php serves as the main call entry for the plug-in.
The plug-in contains a Class named after the plug-in.
', $ Plug_tem_global); $ plug_tem_global =''. Implode ("
", $ Temarr ).'
';}}
This global $ plug_tem_global; uses a global variable $ plug_tem_global as the input content parameter. modifying this variable is equivalent to modifying the content.
In the main file of the plug-in mechanism, the plug-in Hook acts as an interface to call the plug-in. it is actually a string variable, and its internal value is the name of the array inserted into the current position plug-in.
For example, if the array initialized by the above plug-in is named plug_config, the plug-in hook here is $ plug_config. because this variable needs to be operated inside the class, therefore, global is used to convert it into a global variable.
$plug_point_name='plug_config';global $$plug_point_name;
A loop is used internally to execute all plug-ins suitable for 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(); } }}
The following is a comparison between the effect of using plug-ins and not using plug-ins. haha ~ There used to be a mountain ......