手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

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

首页 > PHP >

纯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条记录相关文章

typecho 插件开发(一) (浏览: 28371, 评论: 6)
推荐一款插件:ScribeFire (浏览: 26789, 评论: 4)
typecho 插件:搜索来源关键字高亮 (浏览: 25567, 评论: 3)
jQuery简单开发 (浏览: 24153, 评论: 2)
typecho 插件开发(二) (浏览: 22152, 评论: 3)
团队作品:[NEATSheepDog]法师的牧羊犬 (浏览: 20681, 评论: 2)
TinEye插件 (浏览: 19606, 评论: 0)
typecho 插件开发(三) (浏览: 18034, 评论: 0)
typecho SVN更新 (浏览: 17588, 评论: 0)

5条记录访客评论

呵呵,那就是phpcq的作者盗用Kevin McArthur的一本书'pro php patterns,frameworks,testing and more'上反射API的例子

Post by nothing on 2011, August 20, 12:20 PM 引用此文发表评论 #1

拜托,我都写了。纯粹copy。哪里有改过别人的东西。。。
不过。。听你这么一说,我又打开原来的网页,但,已经打不开了。遗憾。
OK,后来我又到phpcq上去看了一下,地址为:http://www.phpcq.com/id-92/,也是这样,我一个字未动。不知道你是怎么认为的。

Post by gouki on 2010, August 1, 8:48 PM 引用此文发表评论 #2

楼主不厚道,盗用别人书上的例子,还把文档注释中的作者声明给改了,bs

Post by ddd on 2010, August 1, 5:16 PM 引用此文发表评论 #3

好文章,顺便帮你点一下广告.

Post by 莫莫 on 2009, October 28, 5:24 PM 引用此文发表评论 #4

实际上实现的是一个逆向查找的功能,一般情况下,都是从使用子类向上追溯,这个是从父类向下追溯,插件系统怎么用这个东西?

Post by yuehei on 2009, October 22, 11:14 AM 引用此文发表评论 #5


发表评论

评论内容 (必填):