PHP implements plug-in pluggable Design Based on reflection mechanism.

Source: Internet
Author: User

PHP implements plug-in pluggable Design Based on reflection mechanism.

This article describes the pluggable Design of PHP plug-ins Based on the reflection mechanism. We will share this with you for your reference. The details are as follows:

Friends who say PHP is equivalent to ASP can stop it. PHP supports reflection and 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

"Homepage" 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

interface IPlugin{static function getname();static function init();static function getMenu();}

3. Internal implementation interface of the plug-in

Plugin1 implementation interface:

<? PhpClass 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... <Br/> ";}}?>

Plugin2 implementation interface:

<? PhpClass 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... <Br/> ";}}?>

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'] . '/plugin';$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 ); // 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

foreach ($menu as $m){echo "<a href='{$m['href']}'>{$m['text']}</a> ";}

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);

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.