I was reading Java Programming Thoughts recently and saw the chapter on type information, which talked about class information and the concept of reflection. By the way, let’s review the reflection tools of php. Here's what the manual says: "PHP 5 has a complete reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods, and extensions. In addition, the reflection API provides methods to take out reflections in functions, classes, and methods." Documentation comment. "Of course the manual is a bit abstract! The so-called reverse engineering is to get detailed information about classes, methods, attributes, parameters, etc., including comments! The text is always so boring, for example
class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; /** * Enter description here ... */ public function myMethod() { echo 'hello 2b'; } } $ref = new ReflectionClass('Foo'); $props = $ref->getProperties(); foreach ($props as $value) { echo $value->getName()."\n"; } //output //foo //bar //baz
ReflectionClass This class returns relevant information about a certain class, such as attributes , methods, namespaces, implementing those interfaces, etc.! In the previous example, ReflectionClass::getProperties returned an array of ReflectionProperty objects. The
ReflectionProperty class reports information about the properties of a class. For example, isDefault isPrivate isProtected isPublic isStatic, etc., the method getName is to get the name of the attribute!
The above is for obtaining attributes, and there are also methods for obtaining class methods, such as
class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; /** * Enter description here ... */ public function myMethod() { echo 'hello 2b'; } } $ref = new ReflectionClass('Foo'); $method = $ref->getMethod('myMethod'); $method->invoke($ref->newInstance());
ReflectionClass::getMethod is a ReflectionMethod type. The ReflectionMethod class reports information about a method, such as isAbstract isPrivate isProtected isPublic isStatic isConstructor, and there is The important methods Invoke and InvokeArgs are the execution methods!
For other objects, you can read the manual, it is not difficult!
What are the uses of reflection?
Reflection is a dynamically running concept. Used together, they can be used to help us analyze other classes, interfaces, methods, properties, methods and extensions. Patterns can also be built, such as dynamic proxies. It is also very common to use reflection in some PHP frameworks, such as kohana and yii. The following is the code of kohana to implement mvc, which uses reflection!
// Start validation of the controller $class = new ReflectionClass(ucfirst(Router::$controller).'_Controller'); // Create a new controller instance $controller = $class->newInstance(); // Load the controller method $method = $class->getMethod(Router::$method); // Execute the controller method $method->invokeArgs($controller, $arguments);
The above code can clearly see the process of this framework! Through Router, you actually process the url class. Through Router, you can get which controller and which method! Then execute the method again!
The above is the collection of information on PHP reflection. We will continue to add relevant information in the future. Thank you for your support of this site!
For more PHP reflection details and sample code related articles, please pay attention to the PHP Chinese website!

解决Java反射异常(ReflectiveOperationException)的方法在Java开发中,反射(Reflection)是一种强大的机制,它允许程序在运行时动态地获取和操作类、对象、方法和属性等。通过反射,我们可以实现一些灵活的功能,比如动态创建对象、调用私有方法、获取类的注解等。然而,使用反射也会带来一些潜在的风险和问题,其中之一就是反射异常(

Golang函数的反射和类型断言的应用和底层实现在Golang编程中,函数的反射和类型断言是两个非常重要的概念。函数的反射可以让我们在运行时动态的调用函数,而类型断言则可以帮助我们在处理接口类型时进行类型转换操作。本文将深入讨论这两个概念的应用以及他们的底层实现原理。一、函数的反射函数的反射是指在程序运行时获取函数的具体信息,比如函数名、参数个数、参数类型等

如何在Java中使用反射调用方法反射是Java语言的一个重要特性,它可以在运行时动态地获取类的信息并操作类的成员,包括字段、方法和构造函数等。使用反射可以在编译时不知道具体类的情况下操作类的成员,这使得我们能够编写更加灵活和通用的代码。本文将介绍如何在Java中使用反射调用方法,并给出具体的代码示例。一、获取类的Class对象在Java中,要使用反射来调用方

Python是一种灵活的编程语言,为开发人员提供了广泛的功能和工具。其强大的功能包括元编程——一种先进的技术,使开发人员能够在运行时动态地操作和生成代码。在本文中,我们将踏上高级Python元编程领域的旅程,特别关注动态代码生成和反射。通过采用这些技术,开发人员可以创建能够适应、修改甚至自省的代码,从而为创建灵活高效的应用程序开启了新的可能性世界。通过探索Python中动态代码生成和反射的概念和实际应用,我们将揭示元编程如何彻底改变开发过程,使开发人员能够生成健壮且高度适应性的代码。了解元编程元

Go语言作为一门静态类型语言,在代码编写时需要明确每个变量的类型。但是,在某些情况下,我们需要对程序中的类型进行动态的分析和操作,这时就需要用到反射机制。反射机制可以在程序运行时动态地获取程序对象的类型信息,并能够对其进行分析和操作,非常有用。但是,Go语言中反射机制也存在一些局限性,下面我们来详细了解一下。反射机制对性能的影响使用反射机制可以大大增强代

Java是一种面向对象的编程语言,代码在编译后不直接变成机器语言,而是转化为字节码。字节码是Java虚拟机(JVM)可以理解的一种二进制形式。因此,在JVM上运行的程序可以在任何平台上运行,这就是Java的跨平台性。Java字节码的特征Java字节码是一种中间代码。编译器将Java源代码转换为字节码并存储在.class文件中。字节码指令可以轻松地转换为指示任

Java底层技术解读:如何实现反射与动态代理引言:Java是一种面向对象的编程语言,在开发过程中,我们经常需要使用到一些底层技术,比如反射和动态代理。本文将介绍反射和动态代理的原理,并给出具体的代码示例,帮助读者更好地理解和运用这两个底层技术。一、反射(Reflection)的原理反射是Java中一种强大而灵活的特性,它使得我们可以在运行时动态地获取和操作一

反射通常被定义为程序在执行时检查自身并修改其逻辑的能力。用不太专业的术语来说,反射是要求一个对象告诉您它的属性和方法,并更改这些成员(甚至是私有成员)。在本课程中,我们将深入探讨如何实现这一点,以及它何时可能有用。一点历史在编程时代的初期,出现了汇编语言。用汇编语言编写的程序驻留在计算机内部的物理寄存器中。通过读取寄存器可以随时检查其组成、方法和值。更重要的是,您可以在程序运行时通过简单地修改这些寄存器来更改程序。它需要对正在运行的程序有一些深入的了解,但它本质上是反思性的。与任何很酷的玩具一样


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
