<?phpecho ' <pre> ';//define a class that tests reflections cqh{public $name = ' CQH '; Private $country = ' China '; Const GENDER = ' man '; Public function say () {echo ' Hello,world '; } Private Function Eat () {echo ' eat '; } public static function drink () {echo ' Drink '; }}/*//Print all the Reflective interface Reflection::export (new Reflectionextension (' Reflection ')); *//*//reflection creates export results for all PHP classes, get_declared_classes can get all declared classes foreach (Get_declared_classes () as $class) {Reflecti On::export (New Reflectionclass ($class)); } *//*//Only reflects the user-defined class foreach (Get_declared_classes () as $class) {$reflectionClass = new Reflectionclass ($CLA SS); if ($reflectionClass->isuserdefined ()) {reflection::export ($reflectionClass); } *//******************************** uses the Reflection lookup plugin ********************************///to define an interface interface IPlugin { public static function GetName (); }//Find all the class function that implements the IPlugin interfaceFindplugins () {$plugins = array (); foreach (Get_declared_classes () as $class) {$reflectionClass = new Reflectionclass ($class); if ($reflectionClass->implementsinterface (' IPlugin ')) {$plugins [] = $reflectionClass; }} return $plugins; }//Determines the member of the class used for the menu function Computemenu () {$menu = array (); foreach (Findplugins () as $plugins) {$reflectionMethod = $plugins->getmethod (' Getmenuitems '); if ($reflectionMethod->isstatic ()) {$items = $reflectionMethod->invoke (null); } else {//If this method is not static, we need an instance $pluginsInstance = $plugins->n Ewinstance (); $items = $reflectionMethod->invoke ($pluginsInstance); } $menu = Array_merge ($menu, $items); } return $menu; }//Determine the members of the class used for the sidebar of the article 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; }//Determine the member of the class of the sidebar function computesidebars () {$sidebars = array (); foreach (Findplugins () as $plugin) {if ($plugin->hasmethod (' Getsidebars ')) { $reflectionMethod = $plugin->getmethod (' Getsidebars '); if ($reflectionMethod->isstAtic ()) {$items = $reflectionMethod->invoke (null); } else {$pluginInstance = $plugin->newinstance (); $items = $reflectionMethod->invoke ($pluginInstance); } $sidebars = Array_merge ($sidebars, $items); }} return $sidebars; }//Create a class that implements the IPlugin interface Mycoolplugin implements IPlugin {public static function GetName () { Return ' Mycoolplugin '; } public static function Getmenuitems () The numeric index array of the {///menu item} (Array ( ' Description ' = ' mycoolplugin ', ' link ' = '/mycoolplugin '); } public static function Getarticles () {///The numeric index array of the article (Array ( ' Path ' = '. ', '/mycoolplugin ', ' title ' = ' This is a really COol article ', ' text ' = = ' This article is cool because ... '); }} $menu = Computemenu (); $sidebars = Computesidebars (); $articles = Computearticles (); Print_r ($menu); Print_r ($sidebars); Print_r ($articles); Echo ' </pre> ';? >
Reflection Lookup plug-in