PHP Reflection Class Reflectionclass usage Analysis _php technique

Source: Internet
Author: User
Tags php programming php regular expression reflection

This example describes the Reflectionclass usage of PHP reflection classes. Share to everyone for your reference, specific as follows:

Let's look at a piece of code:

/** * @name PHP Reflection api--The plug-in system architecture implemented using reflection technology * @author:P hpcq.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-&GT
   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;
} require_once (' plugin.php ');
$menu = Computemenu ();
$articles = Computearticles ();
Print_r ($menu);

 Print_r ($articles);

The plugin.php code is as follows:

<?php
class Mycoolpugin implements Iplugin
{public
 static
 function GetName ()
 {return
  ' Mycoolplugin ';
 }
 public static
 function Getmenuitems ()
 {return
  array (' description ' => ' mycoolplugin ', ' link ' = > '/mycoolplugin ');
 }
 public static
 function Getarticles ()
 {return
  array (' path ' => '/mycoolplugin ', ' title ' => ') This is a really cool article ', ' text ' => xxxxxxxxx);
 }


The code above is an application of the PHP reflection class.

What is a PHP reflection class, as its name suggests, can be understood as a mapping of a class.

As an example:

Class Fuc {//Definition of a classes
 static
 function ec () {
  echo ' I am a class ';
 }
}
$class =new reflectionclass (' fuc '); Create a reflection class of fuc this class

As for $class what is in this reflective class, you can view the manual, which is unknown.

$FUC = $class->newinstance (); Equivalent
to the instantiation of the Fuc class $fuc->ec ();//implementation of methods in FUC EC/
* Final output: I am a class/

And some of the more advanced uses

$ec = $class->getmethod (' EC '); Obtain the EC method in the Fuc class
$fuc = $class->newinstance ();//Instantiate
$ec->invoke ($FUC);   Implementation of EC methods

The procedure above is very familiar. is actually similar to the method of calling the object

But here is the reverse, the method in front of the object in the back

Add : here is recommended a site of the PHP Code online Format tool, you can facilitate the reader on the online compression of the PHP format code to read, convenient and practical !

PHP code online format Landscaping tools:
Http://tools.jb51.net/code/phpformat

More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.