CKTIS / PHP
Closure
Анонимна или стрелкова функция е обект Closure. fromCallable обвива цел, извикваема в текущия обхват.
Версия: PHP 8.4; repository runtime, production runtime not attested
Синтаксис
Closure::fromCallable($callable)Анонимна или стрелкова функция е обект Closure. fromCallable обвива цел, извикваема в текущия обхват. В обикновено затваряне use копира скаларната стойност, а прихващането по референция вижда по-късните промени.
$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]
Примери
<?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")]);
Резултат
[6,12,2]