首頁 後端開發 php教程 PHP變數與類型擴展之反射及其使用

PHP變數與類型擴展之反射及其使用

Nov 21, 2016 pm 04:58 PM

一、概述與安裝

PHP 5 具有完整的反射 API,增加了對類別、介面、函數、方法和擴充進行反向工程的能力。 此外,反射 API 提供了方法來取出函數、類別和方法中的文件註解。

請注意部分內部 API 遺失了反射擴充工作所需的程式碼。 例如,一個內建的 PHP 類別可能遺失了反射屬性的資料。這些少數的情況被認為是錯誤,不過, 正因為如此,它們應該被發現和修復。

使用這些函數不需要安裝,它們是 PHP 核心的一部分。

二、使用範例

在反射文檔中存在著許多例子,通常位於每個類別的 __construct 文件中。

Example  Shell 裡的反射範例(一個終端)

$ php --rf strlen
$ php --rc finfo
$ php --re json
$ php --ri dom

$ php --re json

$ 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 擴充內建的類別

<?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

四、反射類

Reflection — Reflection 類

ReflectionClass — ReflectionClass 類

ReflectionZendExtension — ReflectionZendExtension 類

ReflectionExtension — ReflectionExtension 類別

ReflectionFunction — ReflectionFunction 類別

ReflectionFunctionAbstract — ReflectionFunctionAbstract 類別

. ReflectionParameter — ReflectionParameter 類別

ReflectionProperty — ReflectionProperty 類別

Reflector — Reflector 介面

ReflectionException — ReflectionException 類別

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles