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

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

WBOY
Libérer: 2016-05-20 11:40:44
original
1181 Les gens l'ont consulté

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

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal