zoukankan      html  css  js  c++  java
  • 反射api获取源代码

    使用反射 api获取类或者类中方法的源代码

    class ReflectionUtil
    {
    	public static function getClassSource(ReflectionClass $reflection)
    	{
    		$file = $reflection->getFileName();
    		$lines = file($file);
    		$start-line = $reflection->getStartLine();
    		$end-line = $reflection->getEndLine();
    		return implode(array_slice($lines, $start-line - 1, $end-line - $start-line + 1));
    	}
    
    	public static function getMethodSource(ReflectionMethod $reflection)
    	{
    		$file = $reflection->getFileName();
    		$lines = file($file);
    		$start-line = $reflection->getStartLine();
    		$end-line = $reflection->getEndLine();
    		return implode(array_slice($lines, $start-line - 1, $end-line - $start-line + 1));
    	}
    }
    

      两个方法惊人的相似。。。。

    另外

    class Test
    {
    	public function testFunction()
    	{
    		return 0;
    	}
    }
    
    $reflectionClass = new ReflectionClass('Test');
    $reflectionMethod = $reflectionClass->getMethod('testFunction');
    $reflectionMethods = $reflectionClass->getMethods();
    

      其中,$reflectionMethod即为ReflectionMethod类的对象。而$reflectionMethods则为对象的集合。

    $reflectionParameters = $reflectionMethod->getParameters();
    

      其中$reflectionParameters则为ReflectionParameter对象数组。

  • 相关阅读:
    Node.js 笔记03
    Node.js 笔记02
    Node.js 笔记01
    源代码管理工具-git
    ES6笔记01
    07_查找、软链接、打包压缩、软件安装
    06_系统信息相关命令
    oracle序列中cache和nocache
    PL/SQL规范、块、过程、函数、包、触发器
    对Xcode菜单选项的详细探索(干货)
  • 原文地址:https://www.cnblogs.com/buerr/p/7305540.html
Copyright © 2011-2022 走看看