Closure call

Closures are anonymous functions that are declared inline and assigned to a variable. It can be used as a callback for later execution. In PHP 5 it was already possible to bind an object to the scope of the closure as if it was a method.

The “call” method is one of the PHP 7 features that was introduced to simplify the process.

<?php
     /**
      * Closure::call()
      */
     class PHPCode {
         private $foo = 'PHPCode';
     }
     
     $getFooCallback = function() {
         return $this->foo;
     };

     echo $getFooCallback->call(new PHPCode);

Leave a Reply

Your email address will not be published. Required fields are marked *