This example for you to share the PHP reflection api--using reflection technology to implement the plug-in system framework for your reference, the specific content as follows
<?php/** * @name PHP Reflection api--The plug-in system architecture implemented using reflection technology * */** * First invoke the Findplugins method to obtain a method to obtain the class * that implements the interface and then call the Reflection class * @param $meth Od method name * @param $interfaceName Interface Name * @return The return result corresponding to the array method name * * Function Compute ($method, $interfaceName) {$findPl
Ugins = Findplugins ($interfaceName);
$menu = Array (); foreach ($findPlugins as $plugin) {//Here gets all classes that implement the IPlugin interface ($plugin->hasmethod ($method)) {//check whether the specific method in the class is set
Yi. $reflectionMethod = $plugin->getmethod ($method); Gets the method in the class if ($reflectionMethod->isstatic ()) {//Determines whether the method is a static method $items = $reflectionMethod->invoke (nu
ll); else {$pluginInstance = $plugin->newinstance (); Creates a new instance of the class.
The given parameter is passed to the class constructor.
$items = $reflectionMethod->invoke ($pluginInstance);
$menu = Array_merge ($menu, Is_array ($items) $items: [$items]);
} return $menu; /** * First finds the class that implements the IPlugin interface from a bunch of defined classes * and then stores it in an array return * @param string $interfaceName * @return Array $plug
Ins*/function Findplugins ($interfaceName) {$plugins = array (); Returns an array of the names of the defined classes, foreach (Get_declared_classes () as $class) {$reflectionClass = new Reflectionclass ($class);//Get CL Ass Reflection object, including the private property method if ($reflectionClass->implementsinterface ($interfaceName)) {//Check whether it implements the IPlugin interface $plugin S[] = $reflectionClass;
Find the class that needs reflection.} return $plugins; Interface iplugin{public static function GetName (); Defining interfaces and Static methods}//Implementing Iplugin Interface class Mycoolpugin implements Iplugin {public static function GetName () {return ' Mycoolp
Lugin '; Public Function Getmenuitems () {//Get menu item return Array (' description ' => ' mycoolplugin ', ' link ' => '/mycoolplu
Gin ')); The public static function Getarticles () {//Gets the article return array (' path ' => '/mycoolplugin ', ' title ' => ' it ')
Really cool article ', ' text ' => ' xxxxxxxxx ');
} $menu = Compute (' getmenuitems ', ' Iplugin ');
$articles = Compute (' getarticles ', ' Iplugin ');
Print_r ($menu); echo "
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.