CKTIS / PHP
Closure
Anonimna ili streličasta funkcija objekt je Closure. fromCallable omata cilj pozivljiv u trenutačnom dosegu.
Verzija: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
Closure::fromCallable($callable)Anonimna ili streličasta funkcija objekt je Closure. fromCallable omata cilj pozivljiv u trenutačnom dosegu. U običnom zatvorenju use kopira skalarnu vrijednost, a obuhvat referencom vidi kasnije promjene.
$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]
Primjeri
<?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")]);
Rezultat
[6,12,2]