PHP 通過反射獲取函數體
<?php
function hello()
{
var_dump("hello");
}
function get_function_define($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));
}
get_function_define("hello");
這樣就能拿到函數體了。