Register Now

Login

Lost Password

Enter your email to reset your password.

Closure::call() is an another new and cool feature added to PHP 7. It allows to bind a closure to a specific object on run time with the addition of closure->call() function.

It is very fast in performance than PHP 5.6 bindTo.

Let’s understand with the help of example –

Before PHP 7 –

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

   // Define a closure code used in before of PHP 7
   $getValue = function() {
      return $this->x;
   };

   // Bind a clousure
   $value = $getValue->bindTo(new A, 'A'); 

   print($value());
?>

Output:
1

In PHP 7 –

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

   // PHP 7+ code
   $value = function() {
      return $this->x;
   };

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

Output:
1

Leave a reply