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.