查看PHP闭包的源代码 [英] View a PHP Closure's Source

查看:124
本文介绍了查看PHP闭包的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以反映或查看PHP闭包对象的来源?也就是说,如果我做这样的事情

Is it possible to reflect into or otherwise view the source of a PHP closure object? That is, if I do something like this

$closure = function()
{
    return 'Hi There';
};

然后是类似的东西

var_dump($closure);

PHP输出

object(Closure)[14]

也就是说,我知道对象是一个闭包,但我不知道它会做什么。

That is, I know the object's a closure, but I have no idea what it does.

我正在寻找一种反射方法,函数或调试扩展,使我可以转储匿名函数的实际主体。

I'm looking for a reflection method, function, or debugging extension that will allow me to dump the actual body of anonymous function.

推荐答案

从PHP可以得到的东西是有限的,通过反射,您只能获取函数的参数签名以及源代码文件的开始和结束行。我曾经写过一篇关于此的博客文章: http://www.metashock.de/2013/05/dump-source-code-of-closure-in-php/ ...

What you can get from PHP is limited, using reflection you can just obtain the parameter signature of the function and the start and ending line of the source code file. I've once wrote a blog article about that: http://www.metashock.de/2013/05/dump-source-code-of-closure-in-php/ ...

使用反射将其引导至以下代码:

It lead me to the following code, using reflection:

function closure_dump(Closure $c) {
    $str = 'function (';
    $r = new ReflectionFunction($c);
    $params = array();
    foreach($r->getParameters() as $p) {
        $s = '';
        if($p->isArray()) {
            $s .= 'array ';
        } else if($p->getClass()) {
            $s .= $p->getClass()->name . ' ';
        }
        if($p->isPassedByReference()){
            $s .= '&';
        }
        $s .= '$' . $p->name;
        if($p->isOptional()) {
            $s .= ' = ' . var_export($p->getDefaultValue(), TRUE);
        }
        $params []= $s;
    }
    $str .= implode(', ', $params);
    $str .= '){' . PHP_EOL;
    $lines = file($r->getFileName());
    for($l = $r->getStartLine(); $l < $r->getEndLine(); $l++) {
        $str .= $lines[$l];
    }
    return $str;
}

如果您有以下关闭项:

$f = function (Closure $a, &$b = -1, array $c = array())
  use ($foo) 
{
    echo $this->name;
    echo 'test';
};

closure_dump()将给出以下结果:

function (Closure $a, &$b = -1, array $c = array (
)){
 use ($foo)
{
    echo $this->name;
    echo 'test';
};

您会发现它是不完善的(数组参数)。同样,它不能正确处理某些边缘情况,尤其是如果闭包是嵌套的,或者多个内联闭包将在一行中传递给一个函数,则尤其如此。后者对我来说最成问题。因为,您仅从反射获得了开始和结束行,所以在这种情况下,两个函数都将位于该行上,并且您没有有用的信息来确定应该转储其中的一个。到目前为止,我还没有找到解决方案,也不确定是否有解决方案。

You see it is imperfect (the array param). Also it will not handle some edge cases properly, especially if closures are nested or multiple inline closures will getting passed to a function in one line. The latter looks most problematic to me. Since, you get only the starting and ending line from reflection, both functions will be on that line in this case and you have no useful information to decide which one of them should get dumped. So far, I didn't found a solution for that, also I'm unsure if there is a solution.

但是,在大多数情况下,至少应该只要您不依赖调试,对调试很有帮助。 随时对其进行增强!

However, in most cases, it should at least being helpful for debugging, as long as you don't rely on it. Feel free to enhance it!

这篇关于查看PHP闭包的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆