PHP Reflection Class Reflectionclass usage analysis, Reflectionclass usage
The example of PHP reflection class Reflectionclass usage is described in this paper. Share to everyone for your reference, as follows:
Take a look at the code first:
/** * @name PHP Reflection api--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 Reflection Class ($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 (' Getart Icles ') {$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 ();p rint_r ($menu);p rint_r ($articles);
The plugin.php code is as follows:
<?phpclass Mycoolpugin implements iplugin{public static function GetName () { return ' mycoolplugin ';} public Stati C function Getmenuitems () { return Array (' description ' = ' mycoolplugin ', ' link ' = '/mycoolplugin ');} public static function Getarticles () { return Array (' path ' = '/mycoolplugin ', ' title ' = ' = ' This is a reall Y cool article ', ' text ' = + xxxxxxxxx); }}
The above code is an application of the PHP reflection class.
What is a PHP reflection class, as the name implies, can be understood as a mapping of a class.
As an example:
Class Fuc {//define a static function EC () { echo ' I am a class ';}} $class =new reflectionclass (' fuc '); Set up a reflection class for the Fuc class
As for the $class of this reflective class, you can view the manual, here is an unknown solution
$FUC = $class->newinstance (); Equivalent to instantiating the Fuc class $fuc->ec (); Execute the method in Fuc ec/* final output: I am a class */
There are some more advanced uses.
$ec = $class->getmethod (' EC '); Obtain the EC method in the Fuc class $fuc= $class->newinstance (); Instantiation of $ec->invoke ($FUC); Implementation of the EC method
The above process is familiar. It's actually similar to the method that invokes the object
But here is the opposite, the method before, the object in the back
Add : here is recommended a site of the PHP Code online Format tool, you can facilitate the reader on-line compressed PHP format code after reading, convenient and practical !
PHP Code online format Beautification tool:
Http://tools.jb51.net/code/phpformat
More about PHP related content readers can view this site topic: "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Math Skills Summary, "PHP Regular Expression Usage summary", "PHP Operations and Operator Usage Summary", "PHP string Usage Summary" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1125867.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125867.html techarticle PHP Reflection Class Reflectionclass usage analysis, Reflectionclass usage This article describes the PHP reflection class Reflectionclass usage. Share to everyone for your reference, as follows: First Look ...