Home > Backend Development > PHP Tutorial > Instructions for using the architecture plug-in using reflection technology in PHP_PHP Tutorial

Instructions for using the architecture plug-in using reflection technology in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:37:36
Original
730 people have browsed it

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 only exists in the plug-in. They will only be used if there is an interface.
Assume you have such an interface

Copy the code The code is as follows:

interface IPlugin{
function getMenuItems();
function getArticles();
function getSideBars();
}
class Someplugin implementlents IPlugin{
public function getMenuItems(){
//No menu items
return null;
}
public function getArticles(){ //No articles
return null;
}
public function getSidBars() {
//With side
return array("sidbarItem');
}
}
[html]
This situation is not very reasonable because it meets the requirements of the interface Requirements, adding unused function bodies for a large number of methods. If there are hundreds of methods in the API, this will not work.
The reflection API provides a solution, using the get_declared_classes() function to obtain the current one. Loaded classes and detect which class implements the IPlugin "mark" method.
Write a method to find plugins using reflection here
[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 an individual method, you can use The hasMethod() method of the REfectionClass class.
Determine the members of the class used for the menu
Copy the code The code is 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 getting the instance of the class, you need to check whether the API method can be called statically. If the method is static, you only need to call 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 get the class from the Refectionclass object For an instance,
call its newInstance() method, and then use the invoke() method to return the instance and pass it in.
Determine the members of the class used for articles and sides
Copy the code The code is 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 the code The code is as follows:

class MyCoolPlugin implements IPlugin{
public static function getName(){return 'MyCoolPlugin';}
public static function getMenuItems(){
//Numeric index array of menu items
return array(array('description'=>'MyCoolPlugin','link'=>'/MyCoolPlugin'));
}
public static function getArticles(){
//Articles Numeric index array
return array(array('path'=>'/MyCoolPlugin','title'=>'This is a really cool article',
'text'=>'This article is cool because...'));
}
public static function getSideBars(){
//Article’s sidebar index array
return array(array('sideBars'=>' /MyCoolPlugin'));
}
}

Finally, you can use this plug-in:
Copy code The code is as follows:

$menu=computeArticles();
$sidebars=computeSidebars();
$articles=computeArticles();
print_r($menu) ;
print_r($sidebars);
print_r($articles);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321893.htmlTechArticleThe plug-in method of the reflection API is based on determining the function of the program at runtime, that is, it allows Create optional interface methods and detect this part of the interface on first use...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template