Detailed application of reflection in PHP, _php Tutorial on PHP reflection Application

Source: Internet
Author: User
Tags vars

Detailed application of reflection in PHP, detailed PHP reflection application


Reflection is in PHP running state, extending the parsing PHP program, exporting or extracting detailed information about classes, methods, properties, parameters, etc., including comments. This dynamic acquisition of information and the ability to dynamically invoke an object's methods is called the Reflection API. Reflection is an API that manipulates the meta-model of an object-oriented paradigm and is powerful in helping us to build complex, scalable applications.
The purpose is to automatically load plugins, automatically generate documents, and even to augment the PHP language.
The PHP reflection API consists of several classes that help us to access the program's metadata or to interact with the related annotations. With reflection we can obtain methods such as class implementations, create an instance of a class (unlike the one created with new), Invoke a method (also different from a regular call), pass a parameter, and invoke the static method of the class dynamically.
The Reflection API is a PHP built-in OOP technology extension that includes classes, exceptions, and interfaces that are used in combination to help us analyze other classes, interfaces, methods, properties, methods, and extensions. These OOP extensions are known as reflections.
With Reflectionclass, we can get the following information for the person class:

1) Constant contants
2) Property Names
3) method names static
4) Property Static Properties
5) Namespace Namespace
6) Whether the person class is final or abstract

Then went to see the source of thinkphp, for the implementation of MVC also have different experience thinkphp\lib\core\app.class.php in the Exec method

if (!preg_match ('/^[a-za-z] (\w) *$/', $action)) {//Illegal operation throw new Reflectionexception (); }//Perform the current operation $method = new Reflectionmethod ($module, $action); #查看方法 if ($method->ispublic ()) {$class = new Reflectionclass ($module); #反射控制器//Pre-action if ($class->hasmetho     D (' _before_ '. $action)) {$before = $class->getmethod (' _before_ '. $action);     if ($before->ispublic ()) {$before->invoke ($module); }}//URL parameter binding detects if (C (' Url_params_bind ') && $method->getnumberofparameters () >0) {switch ($_serve       r[' Request_method ') {case ' POST ': $vars = $_post;      Break       Case ' PUT ': Parse_str (file_get_contents (' Php://input '), $vars);      Break     Default: $vars = $_get;     } $params = $method->getparameters ();      foreach ($params as $param) {$name = $param->getname ();      if (Isset ($vars [$name])) {$args [] = $vars [$name]; }elseif ($param->isdefaultvalueavailable ()) {$args [] = $param->getdefaultvalue ();      }else{throw_exception (L (' _param_error_ '). ': '. $name);    }} $method->invokeargs ($module, $args); }else{$method->invoke ($module); #执行我们需要调用函数}//Post-operation if ($class->hasmethod (' _after_ '. $action)) {$     after = $class->getmethod (' _after_ '. $action);     if ($after->ispublic ()) {$after->invoke ($module); }    }

The above is about the application of reflection in PHP, I hope you understand the learning of PHP reflection helpful.

Articles you may be interested in:

    • PHP reflection mechanism to implement dynamic proxy code
    • Schema plug-in usage instructions for using reflection technology in PHP
    • Analyzing the application of reflection in PHP
    • How to use PHP reflection class Reflectionclass and Reflectionobject
    • PHP reflection using examples and the PHP reflection API in Chinese description
    • PHP Reflection Class Reflectionclass, Reflectionmethod use example
    • PHP Reflection Mechanism Usage Example
    • Example of a reflection usage for a PHP class

http://www.bkjia.com/PHPjc/1110066.html www.bkjia.com true http://www.bkjia.com/PHPjc/1110066.html techarticle explain the application of reflection in PHP, detailed PHP Reflection application reflection is in the PHP running state, the extension of the analysis of PHP programs, export or extract information about classes, methods, properties, parameters, etc.

  • 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.