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"; } } "

相关文章:

  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2021-10-26
  • 2022-02-07
猜你喜欢
  • 2021-08-19
  • 2022-01-07
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
相关资源
相似解决方案