>  기사  >  php教程  >  查看闭包函数的函数体 - coder5

查看闭包函数的函数体 - coder5

WBOY
WBOY원래의
2016-05-20 11:40:441144검색

在调试的时候 如果遇到了闭包,很想知道具体是那个闭包。可是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>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.