The plug-in method of the reflection API is based on determining the function of the program at runtime, that is, it allows the creation of optional interface methods and detects this part of the interface method when it is first used. This part of the interface method only exists in the plug-in. They will only be used in the case of partial interfaces.
Assume you have such an interface
interface IPlugin{ function getMenuItems(); function getArticles(); function getSideBars(); } class Someplugin implelents IPlugin{ public function getMenuItems(){ //没有菜单项 return null; } public function getArticles(){ //没有任何文章 return null; } public function getSidBars(){ //有侧边 return array("sidbarItem'); } } [html] 这种情况并不太合理,因为满足了接口的要求,为大量方法添加了不会用到的函数体,如果在API中有数百个方法,这样是行不通的。 反射API提供了一种解决方法,使用get_declared_classes()函数取得当前加载的类,并检测哪个类实现了IPlugin"标记"的方法。 在这里写了一个使用反射查找插件的方法 [code] function findPlugins(){ $plugins=array(); foreach (get_declared_classes() as $class){ $reflectionsClass=new ReflectionClass($class); if($reflectionsClass->implementsInterface('IPlugin')){ $plugins[]=$reflectionsClass; } } return $plugins; }
In order to determine whether a class implements a single method, you can use the hasMethod() method of the REfectionClass class.
Determine the members of the class used for the menu
function computerMenu(){ $menu=array(); foreach (findPlugins() as $plugin){ if($plugin->hasMethod('getMenuItems')){ $reflectionMethod=$plugin->getMethod('getMenuItems'); if($reflectionMethod->isStatic()){ $items=$reflectionMethod->invoke(null); }else{ $pluginInstance=$plugin->newInstance(); $items=$reflectionMethod->invoke($pluginInstance); } $menu=array_merge($menu,$items); } } return $menu; }
After getting the instance of the class, you need to check whether the API method can be statically detected. If the method is static, you only need to call the invoke() function,
as follows public mixed invoke(stdclass object , mixed args=null)
On the other hand, if the method is not static, you need to get an instance of the plug-in to call this method. To get an instance of the class from the Refectionclass object,
call its newInstance() method, and then use Invoke() method, just return the instance and pass it in.
Determine the members of the classes used for articles and sidebars
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; }
Create a reflective plug-in that implements optional features
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...')); } public static function getSideBars(){ //文章的侧边栏索引数组 return array(array('sideBars'=>'/MyCoolPlugin')); } }
Finally you can use this plug-in:
$menu=computeArticles(); $sidebars=computeSidebars(); $articles=computeArticles(); print_r($menu); print_r($sidebars); print_r($articles);
More on using reflection technology in PHP For related articles on the usage instructions of the architecture plug-in, please pay attention to the PHP Chinese website!