Easy Tutorial
❮ Func Xml Get Error Code Func Filesystem Fileowner ❯

PHP Closure::call()

PHP 7 New Features

PHP 7's Closure::call() offers better performance by dynamically binding a closure function to a new object instance and invoking it.

Example

<?php
class A {
    private $x = 1;
}

// Code defining a closure function before PHP 7
$getXCB = function() {
    return $this->x;
};

// Binding 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 output of the above program is:

1
1

PHP 7 New Features

❮ Func Xml Get Error Code Func Filesystem Fileowner ❯