Home  >  Article  >  Backend Development  >  Detailed explanation of design examples of PHP reflection mechanism implementation plug-in

Detailed explanation of design examples of PHP reflection mechanism implementation plug-in

伊谢尔伦
伊谢尔伦Original
2017-07-01 11:25:111633browse

This article mainly introduces the pluggable design of plug-ins implemented in PHP based on the reflection mechanism. It analyzes the functions of plug-ins, the principles of the reflection mechanism and the steps to implement pluggable design in detail in the form of examples. Friends who need it can Refer to the following

The example of this article describes the pluggable design of plug-ins implemented in PHP based on the reflection mechanism. Share it with everyone for your reference, the details are as follows:

Friends who say that PHP and ASP are equivalent can stop here. PHP supports reflection, and it is still very powerful. Okay, let’s start today’s topic.

Function description:

The page has a main navigation menu with several default connections.
Plug-ins are stored in a unified directory. After the plug-in is loaded, the required links will be automatically added to the navigation menu.
The plug-in can perform certain operations when loading.
Dynamic addition and deletion of plug-ins does not require any code changes.

Final effect:

Home page, plug-in 1, plug-in 2

"Home page" is a menu item that comes with the system. "Plugin1" and "Plugin2" are menu items registered by plugins.

Implementation process:

1. File structure

Learn
plugin
plugin1.php
plugin2.php
test.php

After this design, the page entrance is test.php, and the plug-ins are stored in the plugin directory. You can find all of them by traversing the plugin directory. Plug-in.

2. Design plug-in interface

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

3. Plug-in internal implementation interface

Plugin1 implements the interface:

'插件1′,
'href'=>'http://www.google.com'
);
}
static function init(){
echo self::getname() . " 载入中…
"; } } ?>

Plugin2 implements the interface:

'插件2′,
'href'=>'http://www.live.com'
);
}
static function init(){
echo self::getname() . " 载入中…
"; } } ?>

4. Initialize the main navigation menu on the main page

$menu[] = array(
'text'=>'首页',
'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 Perform plug-in injection operations.

// 反射执行方法(注入菜单)
foreach (get_declared_classes() as $class){
$refClass = new ReflectionClass($class);
if($refClass->implementsInterface('IPlugin')){
//插件初始化
$refClass->getMethod('init')->invoke(null);
//获取注入菜单
$menuItem = $refClass->getMethod('getMenu')->invoke(null);
//合并菜单项
$menu = array_merge($menu, array($menuItem));
}
}

7. Main page output menu HTML

foreach ($menu as $m){
echo "{$m['text']} ";
}

Note that Part 6 is the reflection operation of PHP, which is Not very simple. Analyzing the code, a complete reflection operation timing only has 2 lines of code!

$refClass = new ReflectionClass($class);
$menuItem = $refClass->getMethod('getMenu')->invoke(null);

Okay, that’s it for introducing the basic functions of reflection. Of course, PHP's reflection function is more than just that. If you are interested, explore it yourself.

The above is the detailed content of Detailed explanation of design examples of PHP reflection mechanism implementation plug-in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn