Home Backend Development PHP Tutorial Detailed explanation of the application of reflection in php_php skills

Detailed explanation of the application of reflection in php_php skills

May 16, 2016 pm 07:56 PM
php reflection

Reflection is to extend the analysis of PHP programs in the running state of PHP, and export or extract detailed information about classes, methods, properties, 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.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles