php ReflectionClass
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 //获取属性224 protected function getAttr2(){25 26 echo $this->attr2;27 } 28 /**29 * @desc 获取属性330 * @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());//获取构造函数,没有构造函数返回null46 //var_dump($reflection->getDefaultProperties());//获取默认属性,常量属性不包括47 //var_dump($reflection->getDocComment());//获取针对该类的注释,对于类中法中的注释,忽略,没有返回false48 //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');实例化指定的类
View Code