PHP ReflectionClass,phpreflectionclass
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');實例化指定的類
View Code