PHP uses the BS programming mode to implement the plug-in engine function in the CS software programming mode!
'', 'Str2' =>''), 1); * addPlugin ('cleantext', 'strandstr2', array ('str' => '', 'str2' => ''), 2); */function addPlugin ($ tag, $ func, $ args = array (), $ sort = 10) {global $ plugin_arr, $ plugin_meta, $ idx; $ plugin_arr [$ tag] [$ sort] [++ $ idx] = array ('func' => $ func, 'args '=> $ args, 'args _ count' => sizeof ($ args )); $ plugin_meta [$ tag] [$ func] [$ idx] = $ sort ;} /** immediately delete a function in the function set tag * The first parameter is the custom function set tag name * The second parameter is the name of a single function to be deleted from the function set */function r EmovePlugin ($ tag, $ func) {global $ plugin_arr, $ plugin_meta; if (isset ($ plugin_meta [$ tag] [$ func]) {foreach ($ plugin_meta [$ tag] [$ func] as $ idx => $ sort) {unset ($ plugin_arr [$ tag] [$ sort] [$ idx]);} unset ($ plugin_meta [$ tag] [$ func]);} /** delete a function in the function set tag when doPlugin is executed next time (delete the function before executing the plug-in function in doPlugin, and execute the plug-in engine after deletion !) * The first parameter is the tag name of the custom function set * The second parameter is the name of a single function to be deleted from the function set */function addRemovePlugin ($ tag, $ func) {global $ plugin_remove; if (in_array ($ func, (array) $ plugin_remove [$ tag]) return; $ plugin_remove [$ tag] [] = $ func ;} /** the following execution plug-in methods are consistent with the execution plug-ins that return values, * The only difference is that no return value * // ** executes the plug-in engine */function doAction ($ tag, $ args = array () {global $ action_arr, $ action_remove; if (empty ($ action_arr [$ tag]) return; if (isset ($ action_remove [$ Tag]) {foreach ($ action_remove [$ tag] as $ func) {removeAction ($ tag, $ func) ;}} krsort ($ action_arr [$ tag]); foreach ($ action_arr [$ tag] as $ action_sort) {foreach ($ action_sort as $ action_idx) {$ action_idx ['args '] = array_merge ($ action_idx ['args'], $ args); call_user_func_array ($ action_idx ['func'], array_slice ($ action_idx ['args '], 0, $ action_idx ['args _ count']); }}/ ** add a function */function addAction ($ tag, $ Func, $ args = array (), $ sort = 10) {global $ action_arr, $ action_meta, $ idx; $ action_arr [$ tag] [$ sort] [++ $ idx] = array ('func' => $ func, 'args '=> $ args, 'args _ count' => sizeof ($ args); $ action_meta [$ tag] [$ func] [$ idx] = $ sort ;} /** delete the executed function from the plug-in engine */function removeAction ($ tag, $ func) {global $ action_arr, $ action_meta; if (isset ($ action_meta [$ tag] [$ func]) {foreach ($ action_meta [$ tag] [$ func] as $ idx => $ sort) {unset ($ action _ Arr [$ tag] [$ sort] [$ idx]);} unset ($ action_meta [$ tag] [$ func]);} /** add a pre-deletion function, which will be deleted before the function set is called the next time the plug-in engine is executed */function addRemoveAction ($ tag, $ func) {global $ action_remove; if (in_array ($ func, (array) $ action_remove [$ tag]) return; $ action_remove [$ tag] [] = $ func ;} /* extract stars from the sky-expect more in-depth expansion compression... */?>
// Execution example:
// Prepare the testing function to be used for the plug-in engine
Function str2str2 ($ str ){
Return'
P tag start '. $ str.' P tag end
';
}
Function str3str3 ($ str ){
Return'
B tag start '. $ str.' B tag end';
}
// Note: When testing three cases, you must test them one by one. comment out other unnecessary examples during testing, otherwise, the actual comparison of the permission priority of the plug-in engine cannot be seen, resulting in an exception!
Example 1:
// The execution priority of the str2str2 function is smaller than str3str3. here, the str3str3 ($ str) function is executed first and then the str2str2 ($ str) function is executed;
// The actual operation process is as follows:
$ Str = str3str3 ('this is a parameter passed in like all functions in the plug-in. here the execution priority of str3str3 is higher than str2str2 ');
$ Str = str2str2 ($ str );
Echo $ str;
/* View the HTML source code in the output result browser to get the following content:
P tag startThe B tag starts. this is like the parameters passed in by all functions in the plug-in. here, the execution priority of the str3str3 function is higher than that of the str2str2 B tag.P tag ends
*/
AddPlugin ('cleantext', 'str2str2', array ('str' => ''), 1 );
AddPlugin ('cleantext', 'str3str3', array ('str' => ''), 10 );
Echo doPlugin ('cleantext', array ('str' => 'this is like the parameters passed in by all functions in the plug-in. here the execution priority of str3str3 is higher than str2str2 '));
// Example 2:
AddPlugin ('cleantext', 'str2str2', array ('str' => ''), 10 );
AddPlugin ('cleantext', 'str3str3', array ('str' => ''), 1 );
Echo doPlugin ('cleantext', array ('str' => 'this is like the parameters passed in by all functions in the plug-in. here the execution priority of str2str2 is higher than str3str3 '));
/* The source code of the running result HTML page is as follows:
B label startThe P tag starts. this is like the parameters passed in by all functions in the plug-in. here, the execution priority of the str2str2 function is higher than that of the str3str3 P tag ends.
End of label B
*/
// Example 3:
AddPlugin ('cleantext', 'str2str2', array ('str' => ''), 1 );
AddPlugin ('cleantext', 'str3str3', array ('str' => ''), 1 );
Echo doPlugin ('cleantext', array ('str' => 'when the permission sorting value is the same, the subsequent function permission priority is smaller than the previous one, so the first added function is executed first, the execution priority of str3str3 is smaller than str2str2 '));
/* The result of the executed HTML source code is as follows:
B label startP tag start when the permission sorting value is the same, the subsequent function permission priority is smaller than the previous one, so the first added function is executed first. here the execution priority of function str3str3 is less than str2str2 P tag ends
End of label B
*/
// Example of testing the doAction execution plug-in (this plug-in has no return value and only executes it !)
/* Note: This plug-in is an optional value plug-in. Therefore, it is only used for output or direct execution. the priority is the same as that of the doPlugin plug-in. Therefore, this parameter is not described in detail!
Function alertstr ($ str ){
Echo "script" alert ('$ str'); script ";
}
Function alertstr2 ($ str ){
Echo $ str. '1 + 2 ';
}
AddAction ('alert ', 'alertstr', array ('str' => ''), 1 );
AddAction ('alert ', 'alertstr2', array ('str' => ''), 10 );
DoAction ('alert ', array ('str' => 'parameter to be popped up '));
// The result of the running HTML source code is as follows:
// Parameters to be popped up: 1 + 2 script alert ('parameters to be popped up '); script
?>