zoukankan      html  css  js  c++  java
  • PHP通过反射获得类源码

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

  • 相关阅读:
    LINUX云计算40个相关问题
    sql
    补肾
    少吃食品
    抗癌食品
    爱情感言
    分布式事务的处理
    前端
    JAVA->Enum
    Sleuth+Zipkin+Log
  • 原文地址:https://www.cnblogs.com/zemliu/p/2529403.html
Copyright © 2011-2022 走看看