Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / 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;
}本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130524.htm