![](/img/trans.png)
[英]Closure overloading: is it possible to inspect the number of arguments a PHP closure has without executing it?
[英]How to inspect a closure in php?
我有一个正在传递闭包的函数。 我想找出闭包派生自的方法的名称。 当我调用print_r时,它输出:
Closure Object
(
[static] => Array
(
[listener] => Event_Subscriber_Calq@vendor_product_created
[container] => Illuminate\Foundation\Application Object
...
我如何访问该侦听器值? 我试过 ->static、::$static、getStatic(),我想不出任何方法来获取值。
目前,我的计划是使用输出缓冲来捕获 var_dump 的输出。 我不能为此使用 print_r,因为闭包包含对引用自身的对象的引用,而 print_r 需要很长时间来处理递归。 我也不能使用 var_export,因为它没有在输出中包含我想要的值。 所以,这是我的解决方案:
ob_start();
var_dump($closure);
$data = ob_get_clean();
$data = preg_replace('#^([^\n]*\n){4}#', '', $data);
$data = preg_replace('#\n.*#', '', $data);
$data = preg_replace('#.*string.[0-9]+. "(.*)".*#', '\1', $data);
list($class, $method) = explode('@', $data);
这是可怕的。 有没有另一种方法可以做到这一点? 也许使用反射?
我知道这篇文章很旧,但如果有人在寻找信息,你需要使用 ReflectionFunction:
$r = new ReflectionFunction($closure);
var_dump($r, $r->getStaticVariables(), $r->getParameters());
问候,亚历克斯
在最近的一个项目中,我决定采用一种使用包装类的声明式方法。 该类允许设置描述回调源的自由格式字符串,并且可以用作闭包的直接替换,因为它实现了__invoke()
方法。
例子:
use ClosureTools;
$closure = new NamedClosure(
function() {
// do something
},
'Descriptive text of the closure'
);
// Call the closure
$closure();
要访问有关关闭的信息:
if($closure instanceof NamedClosure) {
$origin = $closure->getOrigin();
}
由于原点是自由形式的字符串,因此可以根据用例将其设置为对识别闭包有用的任何内容。
这是类骨架:
<?php
declare(strict_types=1);
namespace ClosureTools;
use Closure;
class NamedClosure
{
/**
* @var Closure
*/
private $closure;
/**
* @var string
*/
private $origin;
/**
* @param Closure $closure
* @param string $origin
*/
public function __construct(Closure $closure, string $origin)
{
$this->closure = $closure;
$this->origin = $origin;
}
/**
* @return string
*/
public function getOrigin() : string
{
return $this->origin;
}
public function __invoke()
{
return call_user_func($this->closure, func_get_args());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.