Application of reflection in php

不言
Release: 2023-03-30 19:48:02
Original
1469 people have browsed it

This article mainly introduces the application of reflection in PHP in detail, what is reflection, and what is the role of reflection. Interested friends can refer to it

Reflection is in the running state of PHP , extend the analysis of PHP programs, 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) 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, and there are also implementations of MVC Different experiences 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 entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Non-recursive method of implementing PHP tree

php encapsulated database functions and usage

The above is the detailed content of Application 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!