Example of using reflection technology plugin in PHP

Source: Internet
Author: User
The reflection-based plug-in architecture is not actually categorized as a pattern, because it is not actually a template, but rather a combination of the concepts of the form of the program architecture.

The plug-in method of the reflection API is implemented based on the functionality of the program at run time, that is, it allows for the creation of an optional interface method and detects this part of the interface method on first use, which is only used if there is a part of the interface in the plug-in.
Suppose you have such an interface

Interface iplugin{function getmenuitems (); function getarticles (); function getsidebars ();} class Someplugin Implelents iplugin{Public Function Getmenuitems () {//No menu item return NULL,} public function Getarticles () {//no article return null;} PU Blic function Getsidbars () {//has a side return array ("Sidbaritem ');}} [HTML] This is not very reasonable, because it satisfies the requirements of the interface, for a large number of methods to add the unused function body, if there are hundreds of methods in the API, this is not feasible. The Reflection API provides a workaround that uses the get_declared_classes () function to obtain the currently loaded class and detects which class implements the IPlugin "tag" method. Here you write a method that uses reflection to find a plug-in [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.
Determining the members of a class for a 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; }


Once an instance of the class is obtained, it is necessary to detect whether the calling API method can be statically detected, and if the method is static, just call 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, to 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-through.
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 an optional feature

Class Mycoolplugin implements iplugin{public static function GetName () {return ' Mycoolplugin ';} public static function get MenuItems () {///menu item with a numeric index array return of array (' description ' = ' mycoolplugin ', ' link ' = '/mycoolplugin ');} public static function Getarticles () {//The number index array of the article (' path ' = '/mycoolplugin ', ' title ' = ' Really cool article ', ' text ' = = ' This article is cool because ... '); } public static function Getsidebars () {////The sidebar index of the article Return Array (Array (' sidebars ' = '/mycoolplugin '));}}

In the end, you can use the plugin as follows:

$menu =computearticles (); $sidebars =computesidebars (); $articles =computearticles (); Print_r ($menu); Print_r ($sidebars); Print_r ($articles);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.