歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 查看閉包函數的函數體

查看閉包函數的函數體

日期:2017/3/1 9:16:37   编辑:Linux編程

在調試的時候 如果遇到了閉包,很想知道具體是那個閉包。可是ide一般不會顯示閉包的函數體。於是從網上抄了個函數用於顯示閉包函數的內容

function my_closure_dump(Closure $c) {
$str = 'function (';
$r = new \ReflectionFunction($c);
$params = array();
foreach($r->getParameters() as $p) {
$s = '';
if($p->isArray()) {
$s .= 'array ';
} else if($p->getClass()) {
$s .= $p->getClass()->name . ' ';
}
if($p->isPassedByReference()){
$s .= '&';
}
$s .= '$' . $p->name;
if($p->isOptional()) {
$s .= ' = ' . var_export($p->getDefaultValue(), TRUE);
}
$params []= $s;
}
$str .= implode(', ', $params);
$str .= '){' . PHP_EOL;
$lines = file($r->getFileName());
for($l = $r->getStartLine(); $l < $r->getEndLine(); $l++) {
$str .= $lines[$l];
}
return $str;
}

Copyright © Linux教程網 All Rights Reserved