$var = function ($message) {
return $message;
};
如果将变量的函数代码以字符串的方式输出呢?
可以用反射函数ReflectionFunction:
Expanding on the suggestion to use the ReflectionFunction, you could use something like this:
$func = new ReflectionFunction('myfunction');
$filename = $func->getFileName();
$start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
$end_line = $func->getEndLine();
$length = $end_line - $start_line;
$source = file($filename);
$body = implode("", array_slice($source, $start_line, $length));
print_r($body);
参看:
https://stackoverflow.com/questions/7026690/reconstruct-get-code-of-php-function
这个函数在Yii2中也有用到:
