PHP ReflectionClass
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 /** 3 * @desc test reflectionclass 4 * @author songweiqing 5 * @create_time 2015-01-7 6 * 7 */ 8 class Test{ 9 public $attr1 = 'attr1'; 10 protected $attr2 = 'attr2'; 11 private $attr3 = 'attr3'; 12 const ATTR4 = 'I AM THE ATTRIBUTE 4'; 13 public static $attr5 = 'attr5'; 14 15 public function __construct(){ 16 17 self::$attr5 = 'I love you baby'; 18 } 19 public function getAttr1(){ 20 21 echo $this->attr1; 22 } 23 //获取属性2 24 protected function getAttr2(){ 25 26 echo $this->attr2; 27 } 28 /** 29 * @desc 获取属性3 30 * @return string 31 */ 32 private function getAttr3(){ 33 echo $this->attr3; 34 } 35 36 public static function getAttr5(){ 37 echo self::$attr5; 38 } 39 } 40 41 $reflection = new ReflectionClass('Test'); 42 //var_dump($reflection->getName());//获取类名getName(); 43 //var_dump($reflection->getConstant("ATTR4"));//获取指定的常量名 44 //var_dump($reflection->getConstants());//获取一组常量名 45 //var_dump($reflection->getConstructor());//获取构造函数,没有构造函数返回null 46 //var_dump($reflection->getDefaultProperties());//获取默认属性,常量属性不包括 47 //var_dump($reflection->getDocComment());//获取针对该类的注释,对于类中法中的注释,忽略,没有返回false 48 //var_dump($reflection->getEndLine());//获取类中最后一行行数 49 //var_dump($reflection->getFileName());//获取定义类的类名 50 //var_dump($reflection->getMethods());//获取所有类中的方法 51 //var_dump($reflection->getProperties());//获取所有属性,不包含常量属性 52 //$instance = $reflection->newInstanceArgs();//实例化反射的该类 53 //$instance = $reflection->newInstance('Test');实例化指定的类