Php Reflection class usage analysis, ReflectionClass usage _ PHP Tutorial

Source: Internet
Author: User
Php Reflection class usage analysis, ReflectionClass usage. Php Reflection class usage analysis, ReflectionClass usage this article describes the php Reflection class usage. For your reference, let's take a look at the usage analysis of php Reflection class and ReflectionClass usage.

This example describes the php Reflection class usage. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.