在调试的时候 如果遇到了闭包,很想知道具体是那个闭包。可是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教程有兴趣的朋友有所帮助。