(PHP 7 >= 7.1.0)

Closure::fromCallableConverts a callable into a closure


public static Closure Closure::fromCallable ( callable $callable )

Create and return a new anonymous function from given callable using the current scope. This method checks if the callable is callable in the current scope and throws a TypeError if it is not.



The callable to convert.

Return Values

Returns the newly created Closure or throws a TypeError if the callable is not callable in the current scope.

add a note add a note

User Contributed Notes 1 note

igorchernin at yahoo dot com
11 months ago
It seems that the result of the "fromCallable" behaves a little bit different then an original Lambda function.

class A {
    private $name;
    public function __construct($name)
        $this->name = $name;

// test callable
function getName()
      return $this->name;
$bob = new A("Bob");

$cl1 = Closure::fromCallable("getName");
$cl1 = $cl1->bindTo($bob, 'A');

//This will retrieve: Uncaught Error: Cannot access private property A::$name
$result = $cl1();
echo $result;

//But for a Lambda function
$cl2 = function() {
    return $this->name;
$cl2 = $cl2->bindTo($bob, 'A');
$result = $cl2();

// This will print Bob
echo $result;
To Top