PHP uses reflection to implement plug-in mechanism, PHP reflection plug-in mechanism
In this paper, we explain how PHP uses reflection to implement plug-in mechanisms. Share to everyone for your reference. The implementation method is as follows:
Copy CodeThe code is as follows: <?php
/**
* @name PHP Reflection api--Plug-in system architecture using reflection technology
*/
Interface iplugin{
public static function GetName ();
}
function Findplugins () {
$plugins = Array ();
foreach (Get_declared_classes () as $class) {
$reflectionClass = new Reflectionclass ($class);
if ($reflectionClass->implementsinterface (' Iplugin ')) {
$plugins [] = $reflectionClass;
}
}
return $plugins;
}
function Computemenu () {
$menu = Array ();
foreach (Findplugins () as $plugin) {
if ($plugin->hasmethod (' Getmenuitems ')) {
$reflectionMethod = $plugin->getmethod (' Getmenuitems ');
if ($reflectionMethod->isstatic ()) {
$items = $reflectionMethod->invoke (null);
} else {
$pluginInstance = $plugin->newinstance ();
$items = $reflectionMethod->invoke ($pluginInstance);
}
$menu = Array_merge ($menu, $items);
}
}
return $menu;
}
function Computearticles () {
$articles = Array ();
foreach (Findplugins () as $plugin) {
if ($plugin->hasmethod (' getarticles ')) {
$reflectionMethod = $plugin->getmethod (' getarticles ');
if ($reflectionMethod->isstatic ()) {
$items = $reflectionMethod->invoke (null);
} else {
$pluginInstance = $plugin->newinstance ();
$items = $reflectionMethod->invoke ($pluginInstance);
}
$articles = Array_merge ($articles, $items);
}
}
return $articles;
}
Class Mycoolpugin implements Iplugin {
public static function GetName () {
Return ' Mycoolplugin ';
}
public static function Getmenuitems () {
Return Array (' description ' = ' mycoolplugin ', ' link ' = '/mycoolplugin ');
}
public static function Getarticles () {
Return Array (' path ' = '/mycoolplugin ', ' title ' = ' = ' This is a really cool article ', ' text ' = ' xxxxxxxxx ');
}
}
$menu = Computemenu ();
$articles = Computearticles ();
Print_r ($menu);
Print_r ($articles);
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/967746.html www.bkjia.com true http://www.bkjia.com/PHPjc/967746.html techarticle PHP uses reflection to implement plug-in mechanism, PHP reflection plug-in mechanism This article describes how PHP uses reflection to implement the plug-in mechanism. Share to everyone for your reference. Concrete Implementation Method ...