What is the role of php reflection

爱喝马黛茶的安东尼
Release: 2023-02-23 12:18:01
Original
3058 people have browsed it

What is the role of 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.

Related recommendations: "PHP Getting Started Tutorial"

Through ReflectionClass, we can get the following information of the Person class:

(1) Constants

(2) Property Property Names

(3) Method Names Static

(4) Property 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 had different experiences with the implementation of MVC. ThinkPHP\Lib\Core\App.class.php exec method.

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 What is the role of php reflection. 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
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!