WEB DEVELOPER SITE
HTMLCSS TUTORIALS PYTHONPHP7JAVA
 

PHP 7 - Closure:: call()


Closure::call() strategy is added as a shorthand method to incidentally tie an object scope to a closure and invoke it. It is a lot quicker in execution when contrasted with bindTo of PHP 5.6.

Example - Pre PHP 7

<?php 

class A { 

private $x = 1; 

} 

//Define a conclusion Pre PHP 7 code 

$getValue = function() { 

return $this->x; 

}; 

//Bind a clousure 

$value = $getValue->bindTo(new A, 'A'); 

print($value()); 

?>

It produces the accompanying program yield −

1 

Example - PHP 7+

<?php 

class A { 

private $x = 1; 

} 

//PHP 7+ code, Define 

$value = function() { 

return $this->x; 

}; 

print($value->call(new A)); 

?>

It produces the accompanying program yield −

1