php怎么架构插件,PHP 反照机制详解 以及插件架构实现

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. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值