<?PHP/** * @name PHP Reflection api--Plug-in system architecture using reflection technology*//** * Call the Findplugins method first to get to the class that implements the interface * and then call the Reflection class method * @param $method Method name * @param $interfaceName Interface name * @return Array Return result for method name*/functionCompute$method,$interfaceName){ $findPlugins= Findplugins ($interfaceName); $menu=Array(); foreach($findPlugins as $plugin){//Here you get all the classes that implement the IPlugin interface if($plugin->hasmethod ($method)) {//checks whether a particular method in the class is defined. $reflectionMethod=$plugin->getmethod ($method);//getting methods in a class if($reflectionMethod->isstatic ()) {//determine if the method is static $items=$reflectionMethod->invoke (NULL); } 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 find the class implementing the IPlugin interface from a bunch of defined classes * and then store it in the array return * @param string $interfaceName * @return Array $plugins*/functionFindplugins ($interfaceName){ $plugins=Array(); //returns an array consisting of the names of the defined classes foreach(get_declared_classes() as $class){ $reflectionClass=NewReflectionclass ($class);//get the Reflection object of class, including the private property method if($reflectionClass->implementsinterface ($interfaceName)) {//Check if it implements the IPlugin interface $plugins[] =$reflectionClass;//find the class that needs reflection } } return $plugins;}Interfaceiplugin{ Public Static functionGetName ();//defining interfaces and Static methods}//implementing the IPlugin interfaceclassMycoolpuginImplementsIplugin { Public Static functionGetName () {return' Mycoolplugin '; } Public functionGetmenuitems () {//Get menu item return Array(Array(' description ' = ' mycoolplugin ', ' link ' = '/mycoolplugin ')); } Public Static functionGetarticles () {//Get articles return Array(Array(' path ' = '/mycoolplugin ', ' title ' = = ' This is a really cool article ', ' text ' = ' xxxxxxxxx ' )); }}$menu= Compute (' getmenuitems ', ' Iplugin ');$articles= Compute (' getarticles ', ' Iplugin ');Print_r($menu);Echo";Print_r($articles);Echo";$name= Compute (' getName ', ' Iplugin ');Print_r($name);/*What is the difference between new class and new Reflectionclass? 1, new $class () Instantiate Class object 2, New Reflectionclass ($class) to get the reflection object of Class (contains The metadata information) difference: 1, new comes out of class, you cannot access his private properties/methods, but reflection can. 2. The object returned by reflection is the metadata object of class (which contains the metadata information of all properties/methods of Class), but not the class itself; Similar to the account file of the class, but the account file is not a person! */Implementation principle: Call Get_declared_classes () first to return the class that has been defined, and then call the Implementsinterface (Stringa $str) method to obtain the class name that the implementation needs to get the interface, and then call its Method (Public,static public)
PHP Reflection API