CKTIS / PHP
Closure
An anonymous or arrow function is a Closure object. fromCallable creates one from a callable in the current visibility scope.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
Closure::fromCallable($callable)An anonymous or arrow function is a Closure object. fromCallable creates one from a callable in the current visibility scope. Ordinary closures capture listed variables by value unless use specifies a reference. Later changes affect a reference capture but leave an ordinary scalar capture unchanged.
$factor=2;
$copy=function(int $n) use ($factor): int { return $factor*$n; };
$live=function(int $n) use (&$factor): int { return $factor*$n; };
$factor=4; $length=Closure::fromCallable("strlen");
echo json_encode([$copy(3),$live(3),$length("AB")]);
[6,12,2]
Examples
<?php
$factor=2;
$copy=function(int $n) use ($factor): int { return $factor*$n; };
$live=function(int $n) use (&$factor): int { return $factor*$n; };
$factor=4; $length=Closure::fromCallable("strlen");
echo json_encode([$copy(3),$live(3),$length("AB")]);
Result
[6,12,2]