Detailed explanation of the definition and usage of reflection in php

伊谢尔伦
Release: 2023-03-12 07:34:02
Original
1209 people have browsed it

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. Its function is very powerful and can help us build complex and scalable applications.

Its uses include: Automatically load plug-ins, automatically generate 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) Constant Contents
2) Property Property Names
3) Method Names static
4) Static Properties
5) Namespace Namespace
6) Whether the Person class is final or abstract

Then I went to look at the source code of thinkphp , I also have different experiences with the implementation of MVC. The exec method in ThinkPHP\Lib\Core\App.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 the detailed content of Detailed explanation of the definition and usage of reflection in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!