ホームページ  >  記事  >  バックエンド開発  >  PHP の Reflection リフレクション メカニズムの概要

PHP の Reflection リフレクション メカニズムの概要

不言
不言オリジナル
2018-07-05 15:59:571217ブラウズ

この記事では主に PHP のリフレクションの仕組みを紹介します。参考になるものがありますので、共有します。困っている友達は参考にしてください。

PHP5 は追加します新機能: リフレクション。この機能により、プログラマーは

リバース エンジニアリング[リバース エンジニアリング]、クラス、インターフェイス、関数、メソッド、および拡張機能[拡張ライブラリのサポート]を行うことができます。

PHP コードを通じて、オブジェクトのすべての情報を取得し、オブジェクトと対話できます。

次の Person クラスを想定します:

 1 class Person { 
 2     /** 
 3      * For the sake of demonstration, we"re setting this private 
 4      */ 
 5     private $_allowDynamicAttributes = false; 
 6      
 7     /** 
 8      * type=primary_autoincrement 
 9      */
 10     protected $id = 0;
 11     
 12     /**
 13      * type=varchar length=255 null
 14      */
 15     protected $name;
 16     
 17     /**
 18      * type=text null19      */
 20     protected $biography;
 21     public function getId() {
 22         return $this->id;
 23     }
 24     public function setId($v) {
 25         $this->id = $v;
 26     }
 27     public function getName() {
 28         return $this->name;
 29     }
 30     public function setName($v) {
 31         $this->name = $v;
 32     }
 33     public function getBiography() {
 34         return $this->biography;
 35     }
 36     public function setBiography($v) {
 37         $this->biography = $v;
 38     }
 39 }

ReflectionClass を通じて、次の Person クラスの情報を取得できます:

  • 定数コンテンツ

  • プロパティ プロパティ名

  • メソッド名

  • 静的プロパティ静的プロパティ

  • NamespaceNamespace

  • Person クラスが Final か Abstract かどうか

クラス名「person」を ReflectionClass に渡すだけです:

1 $class = new ReflectionClass('Person');

* プロパティの取得 (Properties):

1 $properties = $class->getProperties();2 foreach($properties as $property) {
3     echo $property->getName()."\n";4 }
5 // 输出:6 // _allowDynamicAttributes7 // id8 // name9 // biography

デフォルトでは、ReflectionClass はプライベートおよび保護されたプロパティを含むすべてのプロパティを取得します。プライベート属性のみを取得したい場合は、追加のパラメーターを渡す必要があります:

1 $private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

使用可能なパラメーターのリスト:

  • ##ReflectionProperty::IS_STATIC

  • ReflectionProperty::IS_PUBLIC

  • ReflectionProperty::IS_PROTECTED

  • ReflectionProperty::IS_PRIVATE

パブリック プロパティとプライベート プロパティの両方を取得したい場合は、次のように記述します: ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED

$property->getName() を通じてプロパティ名を取得できます。 、getDocComment プロパティのコメントを通じて書き込みアドレスを取得できます。

 1 foreach($properties as $property) {
 2     if($property->isProtected()) { 
 3         $docblock = $property->getDocComment(); 
 4         preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); 
 5         echo $matches[1]."\n"; 
 6     } 
 7 } 
 8 // Output: 
 9 // primary_autoincrement
 10 // varchar
 11 // text
ちょっと信じられないですね。コメントも取得できます。

## メソッドの取得: getMethods() を通じてクラスのすべてのメソッドを取得します。返されるのは、ReflectionMethod オブジェクトの配列です。

これ以上のデモはありません。

## 最後に、ReflectionMethod を通じてクラス内のメソッドを呼び出します。

$data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer");
foreach($data as $key => $value) {    
if(!$class->hasProperty($key)) {        
throw new Exception($key." is not a valid property");
    } 
    if(!$class->hasMethod("get".ucfirst($key))) {        
    throw new Exception($key." is missing a getter");
    } 
    if(!$class->hasMethod("set".ucfirst($key))) {        
    throw new Exception($key." is missing a setter");
    } 
    // Make a new object to interact with
    $object = new Person(); 
    // Get the getter method and invoke it with the value in our data array
    $setter = $class->getMethod("set".ucfirst($key));    
    $ok = $setter->invoke($object, $value); 
    // Get the setter method and invoke it
    $setter = $class->getMethod("get".ucfirst($key));    
    $objValue = $setter->invoke($object); 
    // Now compare
    if($value == $objValue) {        
    echo "Getter or Setter has modified the data.\n";
    } else {        
    echo "Getter and Setter does not modify the data.\n";
   }
}
以上がこの記事の全内容です。皆様の学習に少しでもお役に立てれば幸いです。その他の関連コンテンツについては、PHP 中国語 Web サイトをご覧ください。

関連する推奨事項:

php で Web コンテンツと画像をクロールする方法

PHP インポート進行状況バー クラス

PHP スコープ

以上がPHP の Reflection リフレクション メカニズムの概要の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。