Plug-in pluggable design with PHP Reflection mechanism

Source: Internet
Author: User
PHP Reflection mechanism plug-in pluggable design says PHP and ASP can be equivalent to friends, PHP supports reflection, and it is still very powerful. Now, let's start today's topic. Function description:The page has a main navigation menu with several default connections.
The plug-in is stored in a directory. after the plug-in is loaded, it automatically adds the required link to the navigation menu.
Some operations can be performed when the plug-in is loaded.
You do not need to change the code when adding or deleting a plug-in dynamically. Final effect:Home page, plug-in 1, plug-in 2
The home page is a menu item that comes with the system. Plug-in 1 and plug-in 2 are Menu Items registered by the plug-in. Implementation process: 1. File structure
Learn
Plugin
Plugin1.php
Plugin2.php
After the design of test. php, the page entry is test. php. The plug-ins are stored in the plugin directory. you only need to traverse the plugin directory to find all the plug-ins. 2. design the plug-in interfaceInterface IPlugin {
Static function getname ();
Static function init ();
Static function getMenu ();
} 3. internal implementation interface of the plug-inPlugin1 implementation interface: Class Welcome implements IPlugin {
Static function getname (){
Return 'Welcome (Plugin )';
} Static function getMenu (){
Return array (
'Text' => 'plug-in 1 ′,
'Href '=> 'http: // www.google.com'
);
} Static function init (){
Echo self: getname (). "loading...
";
}
}
?> Plugin2 implementation interface: Class ShowAD implements IPlugin {
Static function getname (){
Return 'show AD (Plugin )';
} Static function getMenu (){
Return array (
'Text' => 'Plug-In 2 ′,
'Href '=> 'http: // www.live.com'
);
} Static function init (){
Echo self: getname (). "loading...
";
}
}
?> 4. initialize the main navigation menu on the home page$ Menu [] = array (
'Text' => 'homepage ',
'Href '=>'/test. php'
); 5. traverse the plug-in directory and load all plug-ins$ PluginPath = $ _ SERVER ['document _ root']. '/in in'; $ dirHd = opendir ($ pluginPath );
While ($ file = readdir ($ dirHd )){
$ PluginFilePath = $ pluginPath. '/'. $ file;
If ($ file! = '.' & $ File! = '..' & Is_file ($ pluginFilePath )){
Include "$ pluginFilePath ";
}
} 6. filter out the plug-ins that implement the IPlugin interface and execute the plug-in injection operation.// Reflection execution method (injection menu)
Foreach (get_declared_classes () as $ class ){
$ RefClass = new ReflectionClass ($ class );
If ($ refClass-> implementsInterface ('iplugin ')){
// Plug-in Initialization
$ RefClass-> getMethod ('init ')-> invoke (null );
// Obtain the injection menu
$ MenuItem = $ refClass-> getMethod ('getmenu ')-> invoke (null );
// Merge menu items
$ Menu = array_merge ($ menu, array ($ menuItem ));
}
} 7. HTML of the output menu on the home pageForeach ($ menu as $ m ){
Echo "{$ m ['text']}";
} Note that the first part is the PHP Reflection operation. is it very easy. When analyzing the code, there are only two lines of code for a complete reflection operation! $ RefClass = new ReflectionClass ($ class );
$ MenuItem = $ refClass-> getMethod ('getmenu ')-> invoke (null); well, the basic functions of reflection are introduced here. Of course, PHP's reflection function is not just that. if you are interested, explore it yourself.

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.