Heim >php教程 >php手册 >查看闭包函数的函数体 - coder5

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-20 11:40:441187Durchsuche

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

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn