PHP Closure::call()
PHP 7's Closure::call() has better performance, dynamically binding a closure function to a new object instance and invoking it.
Examples
Examples
<?php
class A {
private $x = 1;
}
// Define a closure function in versions prior to PHP 7
$getXCB = function() {
return $this->x;
};
// Bind the closure function to class A
$getX = $getXCB->bindTo(new A, 'A');
echo $getX();
print(PHP_EOL);
// PHP 7+ code
$getX = function() {
return $this->x;
};
echo $getX->call(new A);
?>
class A {
private $x = 1;
}
// Define a closure function in versions prior to PHP 7
$getXCB = function() {
return $this->x;
};
// Bind the closure function to class A
$getX = $getXCB->bindTo(new A, 'A');
echo $getX();
print(PHP_EOL);
// PHP 7+ code
$getX = function() {
return $this->x;
};
echo $getX->call(new A);
?>
The above program produces the following output:
1 1
Other Extensions
PHP 7 New Features