You could all you need to write your own, your own closures having getId() or getHash() or something else.
Example ( Demo ):
1: Hello world 2: Hello world
First close (ID: 1), identifier read in the context of the call. Second closure (ID: 2), ID is read from the closure (where self-reference).
code:
<?php class IdClosure { private $callback; private $id; private static $sequence = 0; final public function __construct(Callable $callback) { $this->callback = $callback; $this->id = ++IdClosure::$sequence; } public function __invoke() { return call_user_func_array($this->callback, func_get_args()); } public function getId() { return $this->id; } } $hello = new IdClosure(function($text) { echo "Hello $text\n";}); echo $hello->getId(), ": ", $hello('world'); $hello2 = new IdClosure(function($text) use (&$hello2) { echo $hello2->getId(), ": Hello $text\n";} ); $hello2('world');
I have no clue if this suits your needs, maybe this gives you some ideas. I suggested spl_object_hash , but didn’t understand the discussion of why it doesn’t do this or it works in the end.
source share