使用反射(Reflection)API

<?php
//使用反射(Reflection)API
//ReflectionClass Reflection
	class person{
		  const p=3.14;//類常量,常量不加$
		  static $b=3;
		  protected $name='李四';
		  public $sex='男';
		  protected function say(){
		  }

		  public function walk(){
		  }
		  static function study(){
		  }
	  }

	  $person=new ReflectionClass('person');//實例化反射對象
	  Reflection::export($person);//獲取類的信息
/*
Class [ <user> class person ] {
  @@ D:\wamp\www\1204\t2.php 3-15

  - Constants [1] {
    Constant [ double p ] { 3.14 }
  }

  - Static properties [1] {
    Property [ public static $b ]
  }

  - Static methods [1] {
    Method [ <user> static public method study ] {
      @@ D:\wamp\www\1204\t2.php 13 - 14
    }
  }

  - Properties [2] {
    Property [ <default> protected $name ]
    Property [ <default> public $sex ]
  }

  - Methods [2] {
    Method [ <user> protected method say ] {
      @@ D:\wamp\www\1204\t2.php 8 - 9
    }

    Method [ <user> public method walk ] {
      @@ D:\wamp\www\1204\t2.php 11 - 12
    }
  }
}

*/
  echo $person->getName();//返回反射對象所屬的類名 person 
  echo $person->getFileName();//返回反射對象的類被定義時所處的文件路徑 D:\wamp\www\1204\t2.php

發佈了35 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章