首頁  >  文章  >  後端開發  >  PHP變數與類型擴展之反射及其使用

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

伊谢尔伦
伊谢尔伦原創
2016-11-21 16:58:131068瀏覽

一、概述與安裝

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(&#39;U&#39;, &#39;x&#39;));
?>
以上例程的输出类似于:
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
上一篇:PHP擴充—OAuth下一篇:PHP擴充—OAuth