首页 > 后端开发 > php教程 > 查看闭包函数的函数体 函数图像 反函数 对数函

查看闭包函数的函数体 函数图像 反函数 对数函

WBOY
发布: 2016-07-29 08:52:36
原创
1484 人浏览过

在调试的时候 如果遇到了闭包,很想知道具体是那个闭包。可是ide一般不会显示闭包的函数体。于是从网上抄了个函数用于显示闭包函数的内容

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

以上就介绍了查看闭包函数的函数体,包括了函数,查看方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板