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:111743browse

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:

<?php
Class Welcome implements IPlugin{
static function getname(){
return &#39;Welcome (Plugin)&#39;;
}
static function getMenu(){
return array(
&#39;text&#39;=>&#39;插件1′,
&#39;href&#39;=>&#39;http://www.google.com&#39;
);
}
static function init(){
echo self::getname() . " 载入中…<br />";
}
}
?>

Plugin2 implements the interface:

<?php
Class ShowAD implements IPlugin{
static function getname(){
return &#39;Show AD (Plugin)&#39;;
}
static function getMenu(){
return array(
&#39;text&#39;=>&#39;插件2′,
&#39;href&#39;=>&#39;http://www.live.com&#39;
);
}
static function init(){
echo self::getname() . " 载入中…<br />";
}
}
?>

4. Initialize the main navigation menu on the main page

$menu[] = array(
&#39;text&#39;=>&#39;首页&#39;,
&#39;href&#39;=>&#39;/test.php&#39;
);

5. Traverse the plug-in directory and load all plug-ins

$pluginPath = $_SERVER[&#39;DOCUMENT_ROOT&#39;] . &#39;/plugin&#39;;
$dirHd = opendir($pluginPath);
while ($file = readdir($dirHd)){
$pluginFilePath = $pluginPath . &#39;/&#39; . $file;
if($file!=&#39;.&#39; && $file!=&#39;..&#39; && 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(&#39;IPlugin&#39;)){
//插件初始化
$refClass->getMethod(&#39;init&#39;)->invoke(null);
//获取注入菜单
$menuItem = $refClass->getMethod(&#39;getMenu&#39;)->invoke(null);
//合并菜单项
$menu = array_merge($menu, array($menuItem));
}
}

7. Main page output menu HTML

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

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(&#39;getMenu&#39;)->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