The plug-in method of the reflection API is based on the functionality of the program that is determined at run time, that is, it allows the creation of optional interface methods and detects this part of the interface method for the first time, and is only used if there is such a part of the interface in the plug-in.
Suppose you have such an interface
Copy Code code as follows:
Interface iplugin{
function Getmenuitems ();
function Getarticles ();
function Getsidebars ();
}
Class Someplugin Implelents iplugin{
Public Function Getmenuitems () {
No menu items
return null;
}
Public Function Getarticles () {//no article
return null;
}
Public Function Getsidbars () {
There are side sides
Return Array ("Sidbaritem ');
}
}
[HTML]
This is not so reasonable, because it satisfies the requirements of the interface, adds a function body that is not used for a large number of methods, and is not feasible if there are hundreds of methods in the API.
The Reflection API provides a workaround for using the get_declared_classes () function to get the currently loaded class and to detect which class implements the IPlugin "tag" method.
Here's a way to find plug-ins using reflection
[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
Copy Code code as follows:
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 you get an instance of the class, you need to detect whether the calling API method can be statically instrumented, and if the method is static, just invoke the Invoke () function.
The following 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, and you get an instance of the class from the Refectionclass object.
Call its newinstance () method, and then use the Invoke () method to return the instance to the pass.
To determine the members of a class used for articles and side edges
Copy Code code as follows:
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
Copy Code code as follows:
Class Mycoolplugin implements iplugin{
public static function GetName () {return ' Mycoolplugin ';}
public static function Getmenuitems () {
Array of numeric indices for menu items
return Array (Array (' description ' => ' mycoolplugin ', ' link ' => '/mycoolplugin '));
}
public static function Getarticles () {
An array of numeric indices for articles
Return Array (' path ' => '/mycoolplugin ', ' title ' => '-is-a-really cool article ',
' Text ' => ' This article are cool because ... ');
}
public static function Getsidebars () {
An array of sidebar indices for articles
return Array (Array (' sidebars ' => '/mycoolplugin '));
}
}
The last thing you can do is use this plugin:
Copy Code code as follows:
$menu =computearticles ();
$sidebars =computesidebars ();
$articles =computearticles ();
Print_r ($menu);
Print_r ($sidebars);
Print_r ($articles);