Explain the application _php techniques of reflection in PHP

Source: Internet
Author: User
Tags php language vars

Reflection is in the PHP running state, extended analysis of the PHP program, export or extract information about classes, methods, properties, parameters, etc., including comments. This dynamically acquired information, and the ability to dynamically invoke the method of the object, is called the Reflection API. Reflection is an API that manipulates object-oriented paradigm-oriented metamodel, and is powerful in its ability to help us build complex, scalable applications.
Its uses such as: Automatic loading plug-ins, automatically generate documents, and even can be used to expand the PHP language.
The PHP reflection API consists of a number of classes that help us to access the program's metadata or interact with related annotations. With reflection, we can get examples of how classes implement those methods, create instances of a class (unlike creating with new), call a method (also different from a regular call), pass arguments, and dynamically invoke a static method of a class.
The Reflection API is a PHP-built OOP technology extension that includes classes, exceptions, and interfaces that can be used to help us analyze other classes, interfaces, methods, properties, methods, and extensions. These OOP extensions are called 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

And then went to see the thinkphp source code, for the implementation of MVC also has a different experience thinkphp\lib\core\app.class.php exec method

if (!preg_match ('/^[a-za-z] (\w) *$/', $action)) {//Illegal operation throw new Reflectionexception (); //execute current operation $method = new Reflectionmethod ($module, $action); #查看方法 if ($method->ispublic ()) {$class = new Reflectionclass ($module); #反射控制器//predecessor action if ($class->hasm
     Ethod (' _before_ '. $action)) {$before = $class->getmethod (' _before_ '. $action);
     if ($before->ispublic ()) {$before->invoke ($module); }//URL parameter bindings detect if (C (' Url_params_bind ') && $method->getnumberofparameters () >0) {switch ($_s
       erver[' 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 to understand the learning of the PHP reflection help.

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.