Detailed explanation of the application of reflection in php, detailed explanation of php reflection application_PHP tutorial

WBOY
Release: 2016-07-12 08:57:14
Original
876 people have browsed it

Detailed explanation of the application of reflection in php, detailed explanation of php reflection application

Reflection is to extend the analysis of PHP programs in the running state of PHP, and export or extract information about classes, methods, attributes, Details of parameters, etc., including comments. This function of dynamically obtaining information and dynamically calling methods of objects is called reflection API. Reflection is an API for manipulating meta-models in the object-oriented paradigm. It is very powerful and can help us build complex and scalable applications.
Its uses include: automatically loading plug-ins, automatically generating documents, and can even be used to expand the PHP language.
The PHP Reflection API consists of several classes that help us access the metadata of the program or interact with related annotations. With the help of reflection, we can obtain the methods implemented by the class, create an instance of the class (different from creating with new), call a method (also different from the regular call), pass parameters, and dynamically call the static methods of the class.
Reflection API is PHP’s built-in OOP technology extension, including some classes, exceptions and interfaces. Used together, they can be used to help us analyze other classes, interfaces, methods, properties, methods and extensions. These oop extensions are called reflection.
Through ReflectionClass, we can get the following information of the Person class:

1) Constants
2) Property Property Names
3) Method Method Names static
4) Properties Static Properties
5) Namespace Namespace
​ 6) Whether the Person class is final or abstract

Then I took a look at the source code of thinkphp and had different experiences with the implementation of MVC. The exec method in ThinkPHPLibCoreApp.class.php

if(!preg_match('/^[A-Za-z](\w)*$/',$action)){
    // 非法操作
    throw new ReflectionException();
   }
   //执行当前操作
   $method = new ReflectionMethod($module, $action); #查看方法
   if($method->isPublic()) {
    $class = new ReflectionClass($module); #反射控制器
    // 前置操作
    if($class->hasMethod('_before_'.$action)) {
     $before = $class->getMethod('_before_'.$action);
     if($before->isPublic()) {
      $before->invoke($module);
     }
    }
    // URL参数绑定检测
    if(C('URL_PARAMS_BIND') && $method->getNumberOfParameters()>0){
     switch($_SERVER['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); #执行我们需要调用函数
    }
    // 后置操作
    if($class->hasMethod('_after_'.$action)) {
     $after = $class->getMethod('_after_'.$action);
     if($after->isPublic()) {
      $after->invoke($module);
     }
    }
Copy after login

The above is about the application of reflection in php. I hope it will be helpful for everyone to understand and learn php reflection.

Articles you may be interested in:

  • The code of PHP reflection mechanism to implement dynamic proxy
  • Instructions for using the architecture plug-in using reflection technology in PHP
  • Analyze the application of reflection in php
  • How to use PHP reflection class ReflectionClass and ReflectionObject
  • PHP reflection usage examples and Chinese description of PHP reflection API
  • PHP’s reflection class ReflectionClass, ReflectionMethod usage examples
  • PHP reflection mechanism usage examples
  • PHP class reflection usage examples

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1110066.htmlTechArticle Detailed explanation of the application of reflection in php, detailed explanation of php reflection application reflection is in the running state of PHP, extended analysis of PHP programs, Export or extract detailed information about classes, methods, properties, parameters, etc.,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template