Parse php Reflection applications

Source: Internet
Author: User
This article provides a detailed analysis of php Reflection applications. For more information, see the use of the next reflection:

 Name = $ name ;}} interface Module {function execute ();} class FtpModule implements Module {function setHost ($ host) {print "FtpModule: setHost (): $ host \ n ";} function setUser ($ user) {print" FtpModule: setUser (): $ user \ n ";} function execute () {// something} class PersonModule implements Module {function setPerson (Person $ person) {print "PersonModule: setPerson: {$ person-> name} \ n ";} function execute () {// s Omething} class ModuleRunner {private $ configData = array ("PersonModule" => array ('person '=> 'Bob '), "FtpModule" => array ('host' => 'example. com ', 'user' => 'Anon'); private $ modules = array (); function init () {$ interface = new ReflectionClass ('module '); foreach ($ this-> configData as $ modulename => $ params) {$ module_class = new ReflectionClass ($ modulename); // instantiate ReflectionClass if (! $ Module_class-> isSubclassOf ($ interface) {// check whether the reflection result shows whether the class is a $ interface subclass throw new Exception ("unknown module type: $ modulename "); // if it is not a Module subclass, an exception is thrown.} $ module = $ module_class-> newInstance (); // instantiate an FtpModule or PersonModule object foreach ($ module_class-> getMethods () as $ method) {// Obtain the method $ this-> handleMethod ($ module, $ method, $ params);} array_push ($ this-> modules, $ module ); // put the instantiated module object into the $ modules array} function handl EMethod (Module $ module, ReflectionMethod $ method, $ params) {$ name = $ method-> getName (); // Obtain the method name $ args = $ method-> getParameters (); // Obtain the parameter if (count ($ args) in the method )! = 1 | substr ($ name, 0, 3 )! = "Set") {// The check method must start with set and only return false;} $ property = strtolower (substr ($ name, 3 )); // remove the three letters set from the method name as the parameter if (! Isset ($ params [$ property]) {// if the $ params array does not contain an attribute, false return false ;} $ arg_class = @ $ args [0]-> getClass; // check the first parameter (and unique) data type of the setter method if (empty ($ arg_class )) {$ method-> invoke ($ module, $ params [$ property]);} else {$ method-> invoke ($ module, $ arg_class-> newInstance ($ params [$ property]) ;}}$ test = new ModuleRunner (); $ test-> init ();?>

Second, obtain information in the class through reflection:

  getFileName();  $lines=@file($path);  $from=$class->getStartLine();  $to=$class->getEndLine();  $len=$to-$from+1;  return implode(array_slice($lines,$from-1,$len)); }}$classname="Person";$path="../practice/{$classname}.php";if(!file_exists($path)){  throw new Exception("No such file as {$path}");}require_once($path);if(!class_exists($classname)){ throw new Exception("No such class as {$classname}");}print ReflectionUtil::getClassSource(new ReflectionClass('Person'));?>

The result is: class Person {public $ age; public $ name; function getName () {return "zjx" ;}function getAge () {return 12 ;}function _ toString () {$ rs = $ this-> getName (); $ rs. = "(age ". $ this-> getAge (). ")"; return $ rs ;}}

For more articles on parsing php Reflection applications, refer to the PHP Chinese website!

Related Article

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.