PHP VERSION: 8.2.18
bind_closure_on_call.php
<?php
$a = function () { return $this->x; };
class FooBar { private $x = 3; }
$foobar = new FooBar;
echo "Using bindTo()\n";
$start = microtime(TRUE);
for ($i = 0; $i < 1000000; $i++) {
$x = $a->bindTo($foobar, "FooBar");
$x();
}
$stop = microtime(TRUE);
printf("\nStart: %.8f\nStop: %.8f\nElapsed: %.8f\n", $start, $stop, $stop - $start);
echo "\nUsing call()\n";
$start = microtime(TRUE);
for ($i = 0; $i < 1000000; $i++) {
$a->call($foobar);
}
$stop = microtime(TRUE);
printf("\nStart: %.8f\nStop: %.8f\nElapsed: %.8f\n", $start, $stop, $stop - $start);
Output
Using bindTo()
Start: 1713623198.29060793
Stop: 1713623198.47803402
Elapsed: 0.18742609
Using call()
Start: 1713623198.47805309
Stop: 1713623198.57990193
Elapsed: 0.10184884
SOURCE CODE