This article mainly introduces php Reflection ReflectionClass usage, and analyzes the concept, functions, and usage of php Reflection class in detail based on the instance form, for more information about php Reflection ReflectionClass, see the following example. We will share this with you for your reference. The details are as follows:
Let's take a look at a piece of code:
/*** @ Name PHP Reflection API -- the system architecture of the plug-in implemented by Reflection Technology * @ author: PHPCQ. COM */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 ('getarticle') {$ reflectionMethod = $ plugin-> getMethod ('getarticle'); if ($ reflectionMethod-> isStatic ()) {$ items = $ reflectionMethod-> invoke (null);} else {$ pluginInstance = $ plugin-> newInstance (); $ items = $ reflectionMethod-> invoke ($ pluginInstance);} $ articles = array_merge ($ articles, $ items);} return $ articles;} require_once ('In in. php '); $ menu = computeMenu (); $ articles = computeArticles (); print_r ($ menu); print_r ($ articles );
The plugin. php code is as follows:
<?phpclass MycoolPugin implements Iplugin{ public static function getName() { return 'MycoolPlugin'; } public static function getMenuItems() { return array(array('description' => 'MycoolPlugin', 'link' => '/MyCoolPlugin')); } public static function getArticles() { return array(array('path' => '/MycoolPlugin', 'title' => 'This is a really cool article', 'text' => xxxxxxxxx)); }}
The above code is an application of php Reflection class.
What is a php Reflection class? as the name suggests, it can be understood as a class ING.
For example:
Class fuc {// define a class static function ec () {echo 'I am a class'; }}$ class = new ReflectionClass ('ucc '); // Create a reflection class for the fuc class
For the reflection class $ class, you can view the manual. I will not explain it here.
$ Fuc = $ class-> newInstance (); // It is equivalent to instantiating the fuc class $ fuc-> ec (); // execute the method ec/* in fuc and final output: I am a class */
There are some more advanced usage
$ Ec = $ class-> getmethod ('EC'); // Obtain the ec method in the fuc class $ fuc = $ class-> newInstance (); // instantiate $ ec-> invoke ($ fuc); // execute the ec method
The above process is quite familiar. In fact, it is similar to calling an object.
But here is the opposite, the method is in front, the object is in the back
Supplement: We recommend an Online php code formatting tool on this site to help readers read the compressed php formatting code on the Internet, which is convenient and practical!
Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat