CKTIS / PHP
Moduły i ich granice
Plik PHP może zwracać jawny obiekt wywoływalny lub konfigurację jako moduł. require wykonuje plik i daje zwróconą wartość.
Wersja: PHP 8.4; repository runtime, production runtime not attested
Składnia
return static fn(int $value): int => $value + 1;Plik PHP może zwracać jawny obiekt wywoływalny lub konfigurację jako moduł. require wykonuje plik i daje zwróconą wartość. Kod dołączony dziedziczy zakres zmiennych miejsca dołączenia, więc zależności przekazuj jako argumenty eksportowanej funkcji. Ponowne require_once po załadowaniu daje 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]
Przykłady
<?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]);
Wynik
[36,true]