This article illustrates the plug-in design of PHP based on reflection mechanism. Share to everyone for your reference, specific as follows:
Say PHP and ASP equivalent friends can stop this, PHP support reflection, but also very powerful. OK, let's start today's topic.
Function Description:
The page has a leading navigation menu with a number of default connections inside.
The plugin is stored uniformly in a directory, and the plugin will automatically add the required links to the navigation menu.
The plug-in can perform certain actions when it is loaded.
Dynamic Add and delete plug-ins do not need to change code.
Final effect:
Homepage, plugin 1, plugin 2
"Home Page" is a menu item with the system itself. "Plugins 1" and "Plug-in 2" are menu items registered by the plug-in.
Implementation process:
1. Document structure
Learn
Plugin
plugin1.php
plugin2.php
test.php
After this design, the page entry for test.php, plug-ins are stored in the plugin directory, as long as the traversal plugin directory can find all the plug-ins.
2. Design Plug-in Interface
Interface iplugin{
static function GetName ();
static function init ();
static function GetMenu ();
}
3. Plug-in internal implementation interface
Plugin1 Implementation Interface:
<?php
Class Welcome implements iplugin{
static function GetName () {return
' Welcome (Plugin) ';
}
static function GetMenu () {return
array (
' text ' => ' plugin 1′,
' href ' => ' http://www.google.com '
);
}
static function init () {
echo self::getname (). "Load ... <br/>"
}
? >
Plugin2 Implementation Interface:
<?php
Class Showad implements iplugin{
static function GetName () {return
' show AD (Plugin) ';
}
static function GetMenu () {return
array (
' text ' => ' plugin 2′,
' href ' => ' http://www.live.com ')
);
}
static function init () {
echo self::getname (). "Load ... <br/>"
}
? >
4. Main Page Initialization navigation menu
$menu [] = Array (
' text ' => ' home ', '
href ' => '/test.php '
);
5. Traverse the plugin directory, load all plugins
$pluginPath = $_server[' Document_root ']. '/plugin ';
$dirHd = Opendir ($pluginPath);
while ($file = Readdir ($dirHd)) {
$pluginFilePath = $pluginPath. '/' . $file;
if ($file!= '. ' && $file!= ' ... ' && is_file ($pluginFilePath)) {
include ' $pluginFilePath ';
}
}
6. Filter out the implementation of the IPlugin interface Plug-ins, and perform plug-in injection operations.
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);
Gets the injection menu
$menuItem = $refClass->getmethod (' GetMenu ')->invoke (null);
Merge menu item
$menu = Array_merge ($menu, Array ($menuItem));
}
7. Home Page Output Menu html
foreach ($menu as $m) {
echo ' <a href= ' {$m [' href ']} ' >{$m [' text ']}</a>;
}
Note that the 6th part is the reflection of PHP operation, is not very simple. Analysis of the code, a complete reflection operation time only 2 lines of code!
$refClass = new Reflectionclass ($class);
$menuItem = $refClass->getmethod (' GetMenu ')->invoke (null);
Well, the basic function of reflection is here. Of course, PHP's reflection function is not only so, interested in discovering it yourself.
More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.