Home >Backend Development >PHP Tutorial >View the function body of the closure function Function image Inverse function Logarithmic function

View the function body of the closure function Function image Inverse function Logarithmic function

WBOY
WBOYOriginal
2016-07-29 08:52:361497browse

When debugging, if you encounter a closure, you would like to know which closure it is. However, IDE generally does not display the function body of the closure. So I copied a function from the Internet to display the content of the closure function

<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>

The above introduces the function body of viewing closure function, including functions and viewing aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn