简体   繁体   中英

How to inspect a closure in php?

I have a function that is being passed a Closure. I want to find out the name of the method that closure is derived from. When I call print_r, it outputs this:

Closure Object
(
  [static] => Array
    (
      [listener] => Event_Subscriber_Calq@vendor_product_created
      [container] => Illuminate\Foundation\Application Object
...

How do I acess that listener value? I tried ->static, ::$static, getStatic(), I can't think of any way to get the value.

Currently, my plan is to use output buffering to capture the output from a var_dump. I can't use print_r for this, because the closure contains a reference to and object that references itself, and print_r takes ages to handle the recursion. I also can't use var_export, because it does not include the value I want in the output. So, this is my solution:

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

Which is horrible. Is there another way to do this? Maybe using reflection?

I know this post is old, but in case someone is looking for info, you need to use ReflectionFunction:

$r = new ReflectionFunction($closure);
var_dump($r, $r->getStaticVariables(), $r->getParameters());

Regards, Alex

In a recent project, I decided on a declarative approach by using a wrapper class. The class allows setting a freeform string describing the callback's origin, and can be used as a direct replacement for the closure, as it implements the __invoke() method.

Example:

use ClosureTools;

$closure = new NamedClosure(
    function() {
        // do something
    }, 
    'Descriptive text of the closure'
);

// Call the closure
$closure();

To access information on the closure:

if($closure instanceof NamedClosure) {
    $origin = $closure->getOrigin();
}

Since the origin is a freeform string, it can be set to whatever is useful to identify the closure depending on the use case.

Here is the class skeleton:

<?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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM