Reflection-based plug-inArchitecture is not actually classified as a pattern, because it is not actually a template, but a series of concepts that are combined to form a program architecture.
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 used for the first time. Only They will only be used if these interfaces exist in the plug-in.
Assume that 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_ declare d_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 hasMethod( of the REfectionClass class) )method.
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->is Static ()){ $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 it can staticDetect the API method call. If the method is static, only You 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, from Refectionclass The object obtains an instance of the class,
calls its newInstance() method, and then uses the invoke() method to return the instance and pass it in.
Determine the members of the class used for articles and sides
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 as long as this is done Got:
$menu=computeArticles(); $sidebars=computeSidebars(); $articles=computeArticles(); print_r($menu); print_r($sidebars); print_r($articles);
The above is the detailed content of Detailed explanation of plug-in examples using reflection technology in PHP. For more information, please follow other related articles on the PHP Chinese website!