在调试的时候 如果遇到了闭包,很想知道具体是那个闭包。可是ide一般不会显示闭包的函数体。于是从网上抄了个函数用于显示闭包函数的内容
<span style="color: #0000ff;">function</span> my_closure_dump(Closure <span style="color: #800080;">$c</span><span style="color: #000000;">) { </span><span style="color: #800080;">$str</span> = 'function ('<span style="color: #000000;">; </span><span style="color: #800080;">$r</span> = <span style="color: #0000ff;">new</span> \ReflectionFunction(<span style="color: #800080;">$c</span><span style="color: #000000;">); </span><span style="color: #800080;">$params</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$r</span>->getParameters() <span style="color: #0000ff;">as</span> <span style="color: #800080;">$p</span><span style="color: #000000;">) { </span><span style="color: #800080;">$s</span> = ''<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$p</span>-><span style="color: #000000;">isArray()) { </span><span style="color: #800080;">$s</span> .= 'array '<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$p</span>-><span style="color: #000000;">getClass()) { </span><span style="color: #800080;">$s</span> .= <span style="color: #800080;">$p</span>->getClass()->name . ' '<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$p</span>-><span style="color: #000000;">isPassedByReference()){ </span><span style="color: #800080;">$s</span> .= '&'<span style="color: #000000;">; } </span><span style="color: #800080;">$s</span> .= '$' . <span style="color: #800080;">$p</span>-><span style="color: #000000;">name; </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$p</span>-><span style="color: #000000;">isOptional()) { </span><span style="color: #800080;">$s</span> .= ' = ' . <span style="color: #008080;">var_export</span>(<span style="color: #800080;">$p</span>->getDefaultValue(), <span style="color: #0000ff;">TRUE</span><span style="color: #000000;">); } </span><span style="color: #800080;">$params</span> []= <span style="color: #800080;">$s</span><span style="color: #000000;">; } </span><span style="color: #800080;">$str</span> .= <span style="color: #008080;">implode</span>(', ', <span style="color: #800080;">$params</span><span style="color: #000000;">); </span><span style="color: #800080;">$str</span> .= '){' . <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">; </span><span style="color: #800080;">$lines</span> = <span style="color: #008080;">file</span>(<span style="color: #800080;">$r</span>-><span style="color: #000000;">getFileName()); </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$l</span> = <span style="color: #800080;">$r</span>->getStartLine(); <span style="color: #800080;">$l</span> $r->getEndLine(); <span style="color: #800080;">$l</span>++<span style="color: #000000;">) { </span><span style="color: #800080;">$str</span> .= <span style="color: #800080;">$lines</span>[<span style="color: #800080;">$l</span><span style="color: #000000;">]; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$str</span><span style="color: #000000;">; }</span>