1 <?php
 2     function getSource(ReflectionClass $ref) {
 3         $path = $ref->getFileName();    #获取脚本文件文件名
 4         $file = file($path); #file()方法获取文件内容,并将内容保存在一个数组中,数组每个元素保存一行内容
 5         $start = $ref->getStartLine();    #获取类在脚本中的第一行行号
 6         $end = $ref->getEndLine();    #获取类在脚本中最后一行的行号
 7         $source = implode(array_slice($file, $start - 1, $end - $start + 1));    #拼装类源码
 8         
 9         var_dump($source);
10     }
11 
12     class Person {
13         public $age;
14         private $name;
15         
16         function say() {
17             echo "yes";
18         }
19     }
20     
21     $ref = new ReflectionClass("Person");
22     getSource($ref);
23 ?>

输出

string(145) " class Person { public $age; private $name; function say() { echo "yes"; } } "

posted on 2012-06-01 01:47  ZimZz  阅读(725)  评论(0编辑  收藏  举报