Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / PHP

Moduly a ich hranice

Súbor PHP môže tvoriť modul vracajúci callable alebo konfiguráciu. require ho vyhodnotí a získa výsledok.

Verzia: PHP 8.4; repository runtime, production runtime not attested

Syntax

return static fn(int $value): int => $value + 1;

Súbor PHP môže tvoriť modul vracajúci callable alebo konfiguráciu. require ho vyhodnotí a získa výsledok. Vložený kód dedí rozsah premenných miesta vloženia, preto závislosti odovzdávajte exportovanej funkcii argumentmi. Neskoršie require_once vráti true.

$file=tempnam(sys_get_temp_dir(),"module");file_put_contents($file,'<?php return static fn(int $cents,int $qty):int=>$cents*$qty;');$price=require $file;$again=require_once $file;unlink($file);echo json_encode([$price(12,3),$again]);
[36,true]

Príklady

<?php
$file=tempnam(sys_get_temp_dir(),"module");file_put_contents($file,'<?php return static fn(int $cents,int $qty):int=>$cents*$qty;');$price=require $file;$again=require_once $file;unlink($file);echo json_encode([$price(12,3),$again]);

Výsledok

[36,true]

Zdroje