用反射可以实现,当然@Rodin也说了,我简单的把代码实现了下
<?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);