Instructions for using reflection technology architecture plug-ins in PHP

Source: Internet
Author: User
The reflection-based plug-in architecture is not actually classified as a pattern, because it is not actually a template, but the concept of a program architecture formed by a combination of a pair of columns.

The reflection-based plug-in architecture is not actually classified as a pattern, because it is not actually a template, but the concept of a program architecture formed by a combination of a pair of columns.

The reflection API plug-in method is implemented based on the function of the program at runtime. That is to say, it allows the creation of optional interface methods and detects this interface method when it is used for the first time, these interfaces are used only when the plug-in has these interfaces.
Assume that you have such an Interface
The Code is 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 articles
Return null;
}
Public function getSidBars (){
// Side
Return array ("sidbaritem ');
}
}
[Html]
This is not very reasonable because it satisfies the interface requirements and adds unused function bodies to a large number of methods. If there are hundreds of methods in the API, this will not work.
The reflection API provides a solution. Use the get_declared_classes () function to obtain the currently loaded class and check which class implements the IPlugin "flag" method.
Here I wrote a method to use the reflection lookup 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;
}

To determine whether a class implements a single method, you can use the hasMethod () method of the REfectionClass class.
Determine the member of the class used for the menu
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 obtaining 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 the invoke () function,
Public mixed invoke (stdclass object, mixed args = null)
On the other hand, if the method is not static, You need to obtain an instance of the plug-in to call this method. You need to obtain an instance of the class from the Refectionclass object,
Call its newInstance () method, and then use the invoke () method to return the instance.
Determine the members of the class used for the article and side
The Code is as follows:
Function computeArticles (){
$ Articles = array ();
Foreach (findPlugins () as $ plugin ){
If ($ plugin-> hasMethod ('getarticles ')){
$ ReflectionMethod = $ plugin-> getMethod ('getarticle ');
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 reflection plug-in that implements optional features
The Code is as follows:
Class MyCoolPlugin implements IPlugin {
Public static function getName () {return 'myolin in ';}
Public static function getMenuItems (){
// The number index array of the menu item
Return array ('description' => 'myolin in', 'link' => '/mycoolin in '));
}
Public static function getArticles (){
// Number index array of the article
Return array ('path' => '/mycoolin in', 'title' => 'this is a really cool article ',
'Text' => 'This article is cool because ...'));
}
Public static function getSideBars (){
// The Sidebar index array of the article
Return array ('inclubars' => '/mycoolin in '));
}
}

The plug-in can be used as long as this is done:
The Code is 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.