手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2009年10月21日的文章

PHP反射API--利用反射技术实现的插件系统架构

纯COPY,没仔细看。。。。【先申明,我COPY对这段代码,主要是看他reflectionClass的应用,并非关注插件的应用。。。】

有爱好的人,可以学习一下。。

http://www.e897.com/Article/wlbc/php/200908/5377.html
  1. /** 
  2. * @name  PHP反射API--利用反射技术实现的插件系统架构 
  3. * @author :PHPCQ.COM 
  4. */  
  5. interface Iplugin{  
  6.         public static function getName();  
  7. }  
  8. function findPlugins(){  
  9.         $plugins = array();  
  10.         foreach (get_declared_classes() as $class){  
  11.                 $reflectionClass = new ReflectionClass($class);  
  12.                 if ($reflectionClass->implementsInterface('Iplugin')) {  
  13.                         $plugins[] = $reflectionClass;  
  14.                 }  
  15.         }  
  16.         return $plugins;  
  17. }  
  18. function computeMenu(){  
  19.         $menu = array();  
  20.         foreach (findPlugins() as $plugin){  
  21.                 if ($plugin->hasMethod('getMenuItems')) {  
  22.                         $reflectionMethod = $plugin->getMethod('getMenuItems');  
  23.                         if ($reflectionMethod->isStatic()) {  
  24.                                 $items = $reflectionMethod->invoke(null);  
  25.                         } else {  
  26.                                 $pluginInstance = $plugin->newInstance();  
  27.                                 $items = $reflectionMethod->invoke($pluginInstance);  
  28.                         }  
  29.                         $menu = array_merge($menu,$items);  
  30.                 }  
  31.         }  
  32.         return $menu;  
  33. }  
  34. function computeArticles(){  
  35.         $articles = array();  
  36.         foreach (findPlugins() as $plugin){  
  37.                 if ($plugin->hasMethod('getArticles')) {  
  38.                         $reflectionMethod = $plugin->getMethod('getArticles');  
  39.                         if ($reflectionMethod->isStatic()) {  
  40.                                 $items = $reflectionMethod->invoke(null);  
  41.                         } else {  
  42.                                 $pluginInstance = $plugin->newInstance();  
  43.                                 $items = $reflectionMethod->invoke($pluginInstance);  
  44.                         }  
  45.                         $articles = array_merge($articles,$items);  
  46.                 }  
  47.         }  
  48.         return $articles;  
  49. }  
  50. require_once('plugin.php');  
  51. $menu = computeMenu();  
  52. $articles  = computeArticles();  
  53. print_r($menu);  
  54. print_r($articles);  
  55.   
  56.   
  57. //plugin.php 代码如下  
  58. <?php  
  59. class MycoolPugin implements Iplugin {  
  60.         public static function getName(){  
  61.                 return 'MycoolPlugin';  
  62.         }  
  63.         public static function getMenuItems(){  
  64.                 return array(array('description'=>'MycoolPlugin','link'=>'/MyCoolPlugin'));  
  65.         }  
  66.         public static function getArticles(){  
  67.                 return array(array('path'=>'/MycoolPlugin','title'=>'This is a really cool article','text'=>xxxxxxxxx));  
  68.         }  
  69. }  

Tags: 反射, 插件

小记

今天是服务器上线来首次重启。
将近10分钟的时候让我等的心焦啊。。。以为Reboot失败了。。吓死我了

首页增加了“工作机会”的链接,是我从51job上订阅回来的。稍作整理,每天更新。

由于代码写的仓促,只作了简单的显示。准备做点页面弄的漂亮一点点,呵呵。。。

Tags: 服务器, 就业