Detailed explanation and sample code of reflection in php
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The reflection mechanism allows programs to obtain and modify class information at runtime. It can be used to implement reflection of interfaces and abstract classes: Interface reflection: obtain the interface reflection object through Class.forName() and access its metadata (name, method and field) . Reflection of abstract classes: Similar to interfaces, you can obtain the reflection object of an abstract class and access its metadata and non-abstract methods. Practical case: The reflection mechanism can be used to implement dynamic proxies, intercepting calls to interface methods at runtime by dynamically creating proxy classes.

You can use reflection to access private fields and methods in Go language: To access private fields: obtain the reflection value of the value through reflect.ValueOf(), then use FieldByName() to obtain the reflection value of the field, and call the String() method to print the value of the field . Call a private method: also obtain the reflection value of the value through reflect.ValueOf(), then use MethodByName() to obtain the reflection value of the method, and finally call the Call() method to execute the method. Practical case: Modify private field values and call private methods through reflection to achieve object control and unit test coverage.

Methods to solve Java reflection exceptions (ReflectiveOperationException) In Java development, reflection (Reflection) is a powerful mechanism that allows programs to dynamically obtain and operate classes, objects, methods, properties, etc. at runtime. Through reflection, we can implement some flexible functions, such as dynamically creating objects, calling private methods, obtaining class annotations, etc. However, using reflection also brings some potential risks and problems, one of which is reflection anomalies (

Reflection provides type checking and modification capabilities in Go, but it has security risks, including arbitrary code execution, type forgery, and data leakage. Best practices include limiting reflective permissions, operations, using whitelists or blacklists, validating input, and using security tools. In practice, reflection can be safely used to inspect type information.

Go language reflection allows you to manipulate variable values at runtime, including modifying Boolean values, integers, floating point numbers, and strings. By getting the Value of a variable, you can call the SetBool, SetInt, SetFloat and SetString methods to modify it. For example, you can parse a JSON string into a structure and then use reflection to modify the values of the structure fields. It should be noted that the reflection operation is slow and unmodifiable fields cannot be modified. When modifying the structure field value, the related fields may not be automatically updated.

The reflection feature in the Go language allows a program to inspect and modify the structure of a type at runtime. By using Type, Value and reflect.Kind, we can obtain the type information, field values and methods of the object, and we can also create and modify objects. Specific operation methods include: checking type (TypeOf()), obtaining field value (ValueOf(), FieldByName()), modifying field value (Set()), and creating object (New()).

Answer: Yes, reflection in Go language can implement aspect-oriented programming. Detailed description: Reflection allows a program to modify and inspect its own types and values at runtime. Through reflection, we can create global aspects for the code, which are triggered before and after the function is executed. This allows us to easily add functionality such as logging without modifying existing code. Reflection provides the advantages of code decoupling, scalability, and flexible control, thereby improving application maintainability and reusability.

Using reflection, Go allows the creation of new types. 1. Use reflect.TypeOf() to get the reflect.Type value of an existing type; 2. Use reflect.New() to create a pointer value of a new type; 3. Through *Ptr.Elem( ) to access the actual value; 4. Reflection can also dynamically create new types based on strings, which is used to build flexible and dynamic programs.
