This article mainly introduces PHP using reflection to implement plug-in mechanism, involving the PHP reflection mechanism and plug-in implementation skills, the need for friends can refer to the following
In this paper, we explain how PHP uses reflection to implement plug-in mechanisms. The specific implementation method is as follows
The 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 (The array (' path ' = '/mycoolplugin ', ' title ' = ' = ' This is a really cool article ', ' text ' = ' xxxxxxxxx '); }} $menu = Computemenu (); $articles = Computearticles (); Print_r ($menu); Print_r ($articles);
Summary : The above is the entire content of this article, I hope to be able to help you learn.