博客
关于我
反射查找插件
阅读量:415 次
发布时间:2019-03-06

本文共 4181 字,大约阅读时间需要 13 分钟。

';//定义一个测试反射的类class CQH{ public $name = 'cqh'; private $country= 'china'; const gender = 'man'; public function say() { echo 'hello,world'; } private function eat() { echo 'eat'; } public static function drink() { echo 'drink'; }}/* //打印所有的反射接口 Reflection::export(new ReflectionExtension('reflection')); *//* //反射创建所有的PHP类的导出结果,get_declared_classes可以获取所有已声明的类 foreach(get_declared_classes() as $class) { Reflection::export(new ReflectionClass($class)); } *//* //只反射用户自己定义的类 foreach(get_declared_classes() as $class) { $reflectionClass = new ReflectionClass($class); if($reflectionClass->isUserDefined()) { Reflection::export($reflectionClass); } } */ /********************************使用反射查找插件********************************/ //定义一个接口 interface IPlugin { public static function getName(); } //查到所有实现了IPlugin接口的类 function findPlugins() { $plugins = array(); foreach(get_declared_classes() as $class) { $reflectionClass = new ReflectionClass($class); if($reflectionClass->implementsInterface('IPlugin')) { $plugins[] = $reflectionClass; } } return $plugins; } //确定用于菜单的类的成员 function computeMenu() { $menu = array(); foreach(findPlugins() as $plugins) { $reflectionMethod = $plugins->getMethod('getMenuItems'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); } else { //如果这个方法不是静态的,我们需要一个实例 $pluginsInstance = $plugins->newInstance(); $items = $reflectionMethod->invoke($pluginsInstance); } $menu = array_merge($menu,$items); } return $menu; } //确定用于文章的侧边栏的类的成员 function computeArticles() { $articles = array(); foreach(findPlugins() as $plugin) { if($plugin->hasMethod('getArticles')) { $reflectionMethod = $plugin->getMethod('getArticles'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); } else { $pluginInstance = $plugin->newInstance(); $items = $reflectionMethod->invoke($pluginInstance); } $articles = array_merge($articles,$items); } } return $articles; } //确定侧边栏的的类的成员 function computeSidebars() { $sidebars = array(); foreach(findPlugins() as $plugin) { if($plugin->hasMethod('getSidebars')) { $reflectionMethod = $plugin->getMethod('getSidebars'); if($reflectionMethod->isStatic()) { $items = $reflectionMethod->invoke(null); } else { $pluginInstance = $plugin->newInstance(); $items = $reflectionMethod->invoke($pluginInstance); } $sidebars = array_merge($sidebars,$items); } } return $sidebars; } //创建一个实现了Iplugin接口的类 class MyCoolPlugin implements IPlugin { public static function getName() { return 'MyCoolPlugin'; } public static function getMenuItems() { //菜单项的数字索引数组 return array(array( 'description' => 'MyCoolPlugin', 'link' => '/MyCoolPlugin' )); } public static function getArticles() { //文章的数字索引数组 return array(array( 'path' => './MyCoolPlugin', 'title' => 'This is a really cool article', 'text' => 'This article is cool because...' )); } } $menu = computeMenu(); $sidebars = computeSidebars(); $articles = computeArticles(); print_r($menu); print_r($sidebars); print_r($articles); echo '';?>

转载地址:http://lblkz.baihongyu.com/

你可能感兴趣的文章
MySQL学习-group by和having
查看>>
MySQL学习-MySQL数据库事务
查看>>
MySQL学习-MySQL条件查询
查看>>
MySQL学习-SQL语句的分类与MySQL简单查询
查看>>
MySQL学习-子查询及limit分页
查看>>
MySQL学习-排序与分组函数
查看>>
MySQL学习-连接查询
查看>>
Mysql学习总结(10)——MySql触发器使用讲解
查看>>
Mysql学习总结(11)——MySql存储过程与函数
查看>>
Mysql学习总结(12)——21分钟Mysql入门教程
查看>>
Mysql学习总结(13)——使用JDBC处理MySQL大数据
查看>>
Mysql学习总结(14)——Mysql主从复制配置
查看>>
Mysql学习总结(15)——Mysql错误码大全
查看>>
Mysql学习总结(17)——MySQL数据库表设计优化
查看>>
Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解
查看>>
Mysql学习总结(19)——Mysql无法创建外键的原因
查看>>
Mysql学习总结(20)——MySQL数据库优化的最佳实践
查看>>
Mysql学习总结(21)——MySQL数据库常见面试题
查看>>
Mysql学习总结(22)——Mysql数据库中制作千万级测试表
查看>>
Mysql学习总结(23)——MySQL统计函数和分组查询
查看>>