使用反射(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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章