PHP Closure::call()

PHP 7's Closure::call() has better performance, dynamically binding a closure function to a new object instance and calling the function.

Example

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

// PHP 7 之前版本定义闭包函数代码
$getXCB = function() {
    return $this->x;
};

// 闭包函数绑定到类 A 上
$getX = $getXCB->bindTo(new A, 'A'); 

echo $getX();
echo "<br/>";

// PHP 7+ 代码
$getX = function() {
    return $this->x;
};
echo $getX->call(new A);
?>

The above program execution output result is:

1
1
Continuing Learning
||
<?php class A { private $x = 1; } // PHP 7 之前版本定义闭包函数代码 $getXCB = function() { return $this->x; }; // 闭包函数绑定到类 A 上 $getX = $getXCB->bindTo(new A, 'A'); echo $getX(); echo "<br/>"; // PHP 7+ 代码 $getX = function() { return $this->x; }; echo $getX->call(new A); ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!