CKTIS / PHP
Closure
Uma função anónima ou de seta é um objeto Closure. fromCallable envolve um destino que se possa chamar no âmbito atual.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
Closure::fromCallable($callable)Uma função anónima ou de seta é um objeto Closure. fromCallable envolve um destino que se possa chamar no âmbito atual. Numa função anónima normal use copia o valor escalar, mas a captura por referência vê alterações posteriores.
$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]
Exemplos
<?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")]);
Resultado
[6,12,2]