PHP under the B/s programming mode to implement the C/S software programming mode plug-in engine function!

Source: Internet
Author: User
 ", ' 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 ' and ' $args, ' Args_count ' =>sizeof ($args));  $plugin _meta[$tag] [$func] [$idx]= $sort; }/* Immediately delete a function in the function set label * The first parameter is a custom function set label name * The second parameter is the name of a single function to remove from the set of functions */function Removeplugin ($tag, $func) {Globa  L $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]); The next time you execute Doplugin, remove a function from the function set label (delete the plug-in function in Doplugin before executing it, and then execute the plug-in engine after you delete it!)  * The first parameter is a custom function set label name * The second parameter is the name of a single function to be removed 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 method corresponds to the method of executing plug-in with the return value above, *The only difference is that there is no return value */* * Execute plugin 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 function to plugin engine */function Addaction ($tag, $func, $args =array (), $sort =10) {global $action _arr, $action _meta,$  Idx  $action _arr[$tag] [$sort][++ $idx]=array (' func ' = $func, ' args ' and ' $args, ' Args_count ' =>sizeof ($args));  $action _meta[$tag] [$func] [$idx]= $sort;  }/* * Remove the executed function from the plugin 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-delete function that will be deleted before the function set is called the next time the plug-in engine executes */function addremoveaction ($tag, $func) {global $action _remove;  if (In_array ($func, (array) $action _remove[$tag])) return;  $action _remove[$tag][]= $func; }/* Extract Sky stars-expect deeper extended compression ...*/?>


Examples of implementation are as follows

Prepare the test function to be used for the plug-in engine.
function Str2str2 ($STR) {
Return '

P label start '. $str. ' P tag end

';
}
function Str3str3 ($STR) {
Return ' B label start '. $str. ' B tag End';

}

Note: In testing three examples, be sure to one test, please comment out the other redundant examples, otherwise you will not see the plug-in engine permission priority of the actual contrast results produce abnormal result!

Example one:
The execution priority of the STR2STR2 function is less than STR3STR3, where the STR3STR3 ($STR) function is executed before the STR2STR2 ($STR) function is performed;
The actual operating process is solved as follows:
$str =STR3STR3 (' This is going to be like all the functions passed in the plug-in parameter here the function STR3STR3 the execution priority is higher than str2str2 ');
$str =str2str2 ($STR);
Echo $str;
/* Output results in the browser to view the HTML source code to get the following content:

P label start b tag start this is going to be like all the functions in the plug-in parameter here function STR3STR3 execution priority higher than STR2STR2 B label end P label End


*/
Addplugin (' Cleantext ', ' str2str2 ', Array (' str ' = = '), 1);
Addplugin (' Cleantext ', ' STR3STR3 ', Array (' str ' = = '), 10);
echo doplugin (' Cleantext ', Array (' str ' = ') which is going to be like all functions passed in the plugin parameter here the function STR3STR3 execution priority higher than str2str2 '));
Example two:
Addplugin (' Cleantext ', ' str2str2 ', Array (' str ' = = '), 10);
Addplugin (' Cleantext ', ' STR3STR3 ', Array (' str ' = = '), 1);
echo doplugin (' Cleantext ', Array (' str ' = ') which is going to be like all functions passed in the plugin parameter here the function STR2STR2 execution priority higher than STR3STR3 '));
/* Run result HTML page source code is as follows:
B label start

P Label start this is going to be like the plug-in all the functions passed in the parameters here function Str2str2 execution priority higher than STR3STR3 p label end

End of the B-tag
*/
Example three:
Addplugin (' Cleantext ', ' str2str2 ', Array (' str ' = = '), 1);
Addplugin (' Cleantext ', ' STR3STR3 ', Array (' str ' = = '), 1);
echo doplugin (' Cleantext ', Array (' str ' + ') when the permission sort value is the same size, the subsequent function permission priority is smaller than the preceding so the first added function first executes, here the function Str3str3 execution priority is less than STR2STR2 ' ));
/* After executing the HTML source code results are as follows:
b label start

P label start when the permission sorting value is the same size, the following function permission priority is smaller than the previous reason to add the first function, where the function Str3str3 execution priority is less than the STR2STR2 p label end

b tag end

*/


//test doaction Execute Plug-in example (the plugin does not return a value, only executes!)
/* Note, the plug-in is returned value plug-in, so only used as output or direct execution occasions, priority and Doplugin plug-in priority settings, so not detailed!
Function Alertstr ($str) {
echo "";
}
Function Alertstr2 ($str) {
echo $str. ' 1+2 ';
}
Addaction (' Alert ', ' Alertstr ', Array (' str ' = = '), 1);
Addaction (' Alert ', ' ALERTSTR2 ', Array (' str ' = = '), 10);
doAction (' Alert ', Array (' str ' = ' to eject parameters ')); The
//Run HTML source code results in the following:

//parameter to eject 1+2

?

  • Related Article

    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.