在調試的時候 如果遇到了閉包,很想知道具體是那個閉包。可是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> < <span>$r</span>->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教程有興趣的朋友有所幫助。