PHP反射获取当前函数的内容

<?php
$test = function () {
    echo 'hello world';
};

function closure_dump($closure) {
	try {
		$func = new ReflectionFunction($closure);
	} catch (ReflectionException $e) {
		echo $e->getMessage();
		return;
	}

	$start = $func->getStartLine() - 1;

	$end =  $func->getEndLine() - 1;

	$filename = $func->getFileName();

	echo implode("", array_slice(file($filename),$start, $end - $start + 1));
}

closure_dump($test);

 

posted @ 2015-05-27 22:33  踏雪无痕SS  阅读(612)  评论(0编辑  收藏  举报