Reflection and use of PHP variables and type expansion
1. Overview and installation
PHP 5 has a complete reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods and extensions. Additionally, the Reflection API provides methods to extract documentation comments from functions, classes, and methods.
Please note that some internal APIs are missing code required for reflection extensions to work. For example, a built-in PHP class may be missing data for a reflected property. These rare cases are considered bugs, however, and as such, they should be discovered and fixed.
No installation is required to use these functions, they are part of the PHP core.
2. Usage examples
There are many examples in the reflection documentation, usually in the __construct documentation of each class.
Example A reflection example in Shell (a terminal)
$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom
The output of the above routine Similar to:
Function [ <internal:Core> function strlen ] { - Parameters [1] { Parameter #0 [ <required> $str ] } } Class [ <internal:fileinfo> class finfo ] { - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Methods [4] { Method [ <internal:fileinfo, ctor> public method finfo ] { - Parameters [2] { Parameter #0 [ <optional> $options ] Parameter #1 [ <optional> $arg ] } } Method [ <internal:fileinfo> public method set_flags ] { - Parameters [1] { Parameter #0 [ <required> $options ] } } Method [ <internal:fileinfo> public method file ] { - Parameters [3] { Parameter #0 [ <required> $filename ] Parameter #1 [ <optional> $options ] Parameter #2 [ <optional> $context ] } } Method [ <internal:fileinfo> public method buffer ] { - Parameters [3] { Parameter #0 [ <required> $string ] Parameter #1 [ <optional> $options ] Parameter #2 [ <optional> $context ] } } } } Extension [ <persistent> extension #23 json version 1.2.1 ] { - Constants [10] { Constant [ integer JSON_HEX_TAG ] { 1 } Constant [ integer JSON_HEX_AMP ] { 2 } Constant [ integer JSON_HEX_APOS ] { 4 } Constant [ integer JSON_HEX_QUOT ] { 8 } Constant [ integer JSON_FORCE_OBJECT ] { 16 } Constant [ integer JSON_ERROR_NONE ] { 0 } Constant [ integer JSON_ERROR_DEPTH ] { 1 } Constant [ integer JSON_ERROR_STATE_MISMATCH ] { 2 } Constant [ integer JSON_ERROR_CTRL_CHAR ] { 3 } Constant [ integer JSON_ERROR_SYNTAX ] { 4 } } - Functions { Function [ <internal:json> function json_encode ] { - Parameters [2] { Parameter #0 [ <required> $value ] Parameter #1 [ <optional> $options ] } } Function [ <internal:json> function json_decode ] { - Parameters [3] { Parameter #0 [ <required> $json ] Parameter #1 [ <optional> $assoc ] Parameter #2 [ <optional> $depth ] } } Function [ <internal:json> function json_last_error ] { - Parameters [0] { } } } } dom DOM/XML => enabled DOM/XML API Version => 20031129 libxml Version => 2.7.3 HTML Support => enabled XPath Support => enabled XPointer Support => enabled Schema Support => enabled RelaxNG Support => enabled
3. Related extensions
If you want to create specialized versions of built-in classes (for example, when creating and exporting highlighted HTML, replace methods with easily accessible member variables or use utility methods) , you can continue and extend them.
Example #1 Extending a built-in class
<?php /** * My Reflection_Method class */ class My_Reflection_Method extends ReflectionMethod { public $visibility = array(); public function __construct($o, $m) { parent::__construct($o, $m); $this->visibility = Reflection::getModifierNames($this->getModifiers()); } } /** * Demo class #1 * */ class T { protected function x() {} } /** * Demo class #2 * */ class U extends T { function x() {} } // 输出信息 var_dump(new My_Reflection_Method('U', 'x')); ?> 以上例程的输出类似于: object(My_Reflection_Method)#1 (3) { ["visibility"]=> array(1) { [0]=> string(6) "public" } ["name"]=> string(1) "x" ["class"]=> string(1) "U" }
If you override the constructor, remember to call the parent class's constructor before writing any inserted code. Failure to do so will result in the following results: Fatal error: Internal error: Failed to retrieve the reflection object
4. Reflection class
Reflection — Reflection class
ReflectionClass — ReflectionClass class
ReflectionZendExtension — ReflectionZendExtension class
ReflectionExtension — ReflectionExtension class
ReflectionFunction — ReflectionFunction class
ReflectionFunctionAbstract — ReflectionFunctionAbstract class
ReflectionMethod — ReflectionMethod class
ReflectionObject — ReflectionObject class
ReflectionParameter — ionParameter class
ReflectionProperty — ReflectionProperty class
Reflector — Reflector interface
ReflectionException — ReflectionException class

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

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
