PHP變數與類型擴展之反射及其使用
一、概述與安裝
PHP 5 具有完整的反射 API,增加了對類別、介面、函數、方法和擴充進行反向工程的能力。 此外,反射 API 提供了方法來取出函數、類別和方法中的文件註解。
請注意部分內部 API 遺失了反射擴充工作所需的程式碼。 例如,一個內建的 PHP 類別可能遺失了反射屬性的資料。這些少數的情況被認為是錯誤,不過, 正因為如此,它們應該被發現和修復。
使用這些函數不需要安裝,它們是 PHP 核心的一部分。
二、使用範例
在反射文檔中存在著許多例子,通常位於每個類別的 __construct 文件中。
Example Shell 裡的反射範例(一個終端)
$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom
$ php --ri dom
$ php類似於:
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
三、相關擴充
如果你想建立內建類別的專門版本(比如說,在建立並匯出高亮HTML 時,以易於存取的成員變數來取代方法或使用實用的方法) , 你可以繼續並擴展它們。
Example #1 擴充內建的類別 四、反射類 Reflection — Reflection 類 ReflectionClass — ReflectionClass 類 ReflectionZendExtension — ReflectionZendExtension 類 ReflectionExtension — ReflectionExtension 類別 ReflectionFunction — ReflectionFunction 類別 ReflectionFunctionAbstract — ReflectionFunctionAbstract 類別<?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"
}
如果你重寫了建構函數,記住在寫任何插入的程式碼之前要先呼叫父類別的建構子。 不這麼做將會導致以下的結果: Fatal error: Internal error: Failed to retrieve the reflection object

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

CakePHP 是 PHP 的開源框架。它旨在使應用程式的開發、部署和維護變得更加容易。 CakePHP 基於類似 MVC 的架構,功能強大且易於掌握。模型、視圖和控制器 gu

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

CakePHP 是一個開源MVC 框架。它使應用程式的開發、部署和維護變得更加容易。 CakePHP 有許多函式庫可以減少大多數常見任務的過載。

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲
