This article mainly shares the PHP Reflection API example. The plug-in system architecture implemented by reflection technology has some reference value, interested friends can refer to the example in this article to share with you the PHP Reflection API-the plug-in system architecture implemented using the reflection technology for your reference. The details are as follows:
HasMethod ($ method) {// check whether a specific method in the class is defined. $ ReflectionMethod = $ plugin-> getMethod ($ method); // Obtain the method if ($ reflectionMethod-> isStatic () in the class ()) {// Determine whether the method is a static method $ items = $ reflectionMethod-> invoke (null);} else {$ pluginInstance = $ plugin-> newInstance (); // create a new instance of the class. The given parameters are 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 the defined classes * and store it in the array. return * @ param string $ interfaceName * @ return array $ plugins */ function findPlugins ($ interfaceName) {$ plugins = array (); // returns the array foreach (get_declared_classes () as $ class) consisting of the names of the defined classes) {$ reflectionClass = new ReflectionClass ($ class); // Obtain the reflection object of the class, including the private property method if ($ reflectionClass-> implementsInterface ($ interfaceName )){ // Check whether the Iplugin interface $ plugins [] = $ reflectionClass; // find the class to be reflected} return $ plugins ;} interface Iplugin {public static function getName (); // defines the interface and static method} // implements the Iplugin interface class MycoolPugin implements Iplugin {public static function getName () {return 'myolin in';} public function getMenuItems () {// Obtain the menu item return array ('description' => 'myolplugin ', 'link' => '/mycoolin in');} public static function GetArticles () {// get the article return array ('path' => '/MycoolPlugin', 'title' => 'This is a really cool article ', 'text' => 'xxxxxxxxxx'); }}$ menu = compute ('getmenuitems ', 'iplugin'); $ articles = compute ('getarticle ', '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 Refl EctionClass ($ class) obtains the reflection object of the class (including metadata information). difference: 1. new class. you cannot access its private attributes/methods, but reflection is acceptable. 2. the returned object is the metadata object of the class (including metadata information of all attributes/methods of the class), but not the class itself. similar to the account file of the class, but the account file is not a person! */
The above is all the content of this article. I hope it will help you learn and support PHP.
For more PHP Reflection API examples, please follow the PHP Chinese network!