PHP下用B/S编程模式去实现C/S软件编程模式下的插件引擎功能!
<?php /** * 摘取天上星 版 插件引擎 第二版 version 2.0 * By: 摘取天上星! * Emali: happy.yin@qq.com * Date: 2012升级版 **/ $plugin_arr=array(); $plugin_meta=array(); $plugin_remove=array(); $action_arr=array(); $action_meta=array(); $action_remove=array(); $idx=0; /* * 执行插件引擎中捆绑的所有函数事件(函数执行顺序参加addPlugin函数添加插件时的第四个参数数字,数字越大优先级越高) * $tag 要执行的函数集插件标签名 * $args 要往函数中传入的参数,依次按顺序填写,键名同addPlugin添加插件时第三个参数传入的键名、数量对应一致,键名对应的值即传入的参数值, * 该插件引擎是有返回值的插件引擎 */ function doPlugin($tag,$args=array()){ global $plugin_arr,$plugin_remove; $first=array_search(current($args),$args); if(empty($plugin_arr[$tag])) return $args[$first]; if(isset($plugin_remove[$tag])){ foreach($plugin_remove[$tag] as $func){ removePlugin($tag,$func); } } krsort($plugin_arr[$tag]); foreach($plugin_arr[$tag] as $plugins){ foreach($plugins as $plugins){ $plugins['args']=array_merge($plugins['args'],$args); $args[$first]=call_user_func_array($plugins['func'],array_slice($plugins['args'],0,$plugins['args_count'])); } } return $args[$first]; } /* 第一个参数为自定义标签集名, * 第二个参数是你要向标签集里添加的函数名, * 第三个数组参数为第二个参数strAndStr1函数对应的参数集,有多少个函数参数,就需要添加多少个数组元素, 参数按照先后顺序依次填写,键值为空即可,且插件里所有函数的参数个数必须一致,一个以上的参数,可多个, 这里的传参数组只需要预写好键名即可,在调用doPlugin插件时给对应的键值传入键名对应的实际参数值即可 * 第四个参数为排序参数,从1到10的纯数字,数值越大执行优先级越高,反之越小,默认为值为最大优先级10 * addPlugin('cleanText','strAndStr1',array('str'=>'','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; } /* * 立即删除函数集标签中 的某个函数 * 第一个参数为自定义函数集标签名称 * 第二个参数为要从函数集里 删除的单个函数名称 */ function removePlugin($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]); } } /* * 在下次执行doPlugin时删除函数集标签中 的某个函数(在doPlugin中的插件函数执行前删除,并且删除后执行插件引擎!) * 第一个参数为自定义函数集标签名称 * 第二个参数为要从函数集里 删除的单个函数名称 */ function addRemovePlugin($tag,$func){ global $plugin_remove; if(in_array($func,(array)$plugin_remove[$tag])) return ; $plugin_remove[$tag][]=$func; } /* * 如下执行插件方法同上述有返回值的执行插件使用方法对应一致, * 唯一的区别是没有返回值 */ /* * 执行插件引擎 */ 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'])); } } } /* * 向插件引擎里添加函数 */ 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; } /* * 从插件引擎里删除 执行的函数 */ 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]); } } /* * 添加预删除函数,该函数会在下次执行插件引擎时,在函数集调用前被删除 */ function addRemoveAction($tag,$func){ global $action_remove; if(in_array($func,(array)$action_remove[$tag])) return ; $action_remove[$tag][]=$func; } /* 摘取天上星 - 期待更深层次的扩展压缩...*/?>
//执行例子如下
//为插件引擎准备好要用到的测试函数function str2str2($str){
return '
P标签开始 '.$str.' P标签结束
';}
function str3str3($str){
return 'b标签开始 '.$str.' b标签结束';
}
//注意:在测试三个例子时,一定要一个一个的测试,测试时请注释掉其他多余的例子,否则将无法看到插件引擎权限优先级的 实际对比效果产生异常结果!
例子一://str2str2函数的执行优先级小于str3str3,这里先执行str3str3($str)函数后执行str2str2($str)函数;
//实际运行流程解刨如下:
$str=str3str3('这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2');
$str=str2str2($str);
echo $str;
/*输出结果浏览器里查看HTML源代码得到如下内容:
P标签开始 b标签开始 这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2 b标签结束 P标签结束
*/
addPlugin('cleanText','str2str2',array('str'=>''),1);
addPlugin('cleanText','str3str3',array('str'=>''),10);
echo doPlugin('cleanText',array('str'=>'这是要像插件里所有函数传入的参数这里函数str3str3的执行优先级高于str2str2'));
//例子二:
addPlugin('cleanText','str2str2',array('str'=>''),10);
addPlugin('cleanText','str3str3',array('str'=>''),1);
echo doPlugin('cleanText',array('str'=>'这是要像插件里所有函数传入的参数这里函数str2str2的执行优先级高于str3str3'));
/*运行结果HTML页面源代码如下:
b标签开始
P标签开始 这是要像插件里所有函数传入的参数这里函数str2str2的执行优先级高于str3str3 P标签结束
b标签结束*/
//例子三:
addPlugin('cleanText','str2str2',array('str'=>''),1);
addPlugin('cleanText','str3str3',array('str'=>''),1);
echo doPlugin('cleanText',array('str'=>'当权限排序值大小一致时,后面的函数权限优先级要小于前面的故先添加的函数先执行,这里函数str3str3的执行优先级小于str2str2'));
/* 执行后的HTML源代码结果如下:
b标签开始
P标签开始 当权限排序值大小一致时,后面的函数权限优先级要小于前面的故先添加的函数先执行,这里函数str3str3的执行优先级小于str2str2 P标签结束
b标签结束*/
/*注,该插件为伍返回值插件,故而只用做输出 或直接执行场合,优先级同doPlugin插件优先级设置,故不详述!
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'=>'要弹出的参数'));
//运行后的HTML源代码结果如下:
//要弹出的参数1+2<script>alert('要弹出的参数');</script>
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
