Pluggable PHP Reflection mechanism plug-in design (I)

Source: Internet
Author: User
Pluggable PHP Reflection mechanism plug-in design (I) 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
Test. php
After this design, the page entry is test. php, and 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 interface

?
123456 InterfaceIPlugin {staticfunctiongetname (); staticfunctioninit (); staticfunctiongetMenu ();}
3. internal implementation interface of the plug-in
Plugin1 implementation interface:
?
1234567891011121314151617181920 'Plug-in 1', 'href '=> 'http: // www.google.com');} staticfunctioninit () {echoself: getname (). "loading ..."; }}?>

Plugin2 implementation interface:


?
1234567891011121314151617181920 'Plug-In 2', 'href '=> 'http: // www.live.com');} staticfunctioninit () {echoself: getname (). "loading ..."; }}?>
4. initialize the main navigation menu on the home page



?
1234 $ Menu [] = array ('text' => 'homepage', 'href '=>'/test. php ');
5. traverse the plug-in directory and load all plug-ins



?
12345678910 $ 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)



?
12345678910111213 Foreach (get_declared_classes () as $ class) {$ refClass = newReflectionClass ($ class); if ($ refClass-> implementsInterface ('iplugin ')) {// plug-in initialization $ refClass-> getMethod ('init ')-> invoke (null); // get 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 page



?
1234 Foreach ($ menuas $ 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!


?
12 $ RefClass = newReflectionClass ($ class); $ menuItem = $ refClass-> getMethod ('getmenu ')-> invoke (null );


Now, we will introduce the basic functions of reflection. 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.