KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

Modules en grenzen

Een PHP-bestand kan als module een callable of configuratie teruggeven. require voert het uit en levert de retourwaarde.

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

Syntax

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

Een PHP-bestand kan als module een callable of configuratie teruggeven. require voert het uit en levert de retourwaarde. Ingesloten code erft het variabelebereik van de insluitplaats, dus geef afhankelijkheden als argumenten mee. Een latere require_once geeft 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]

Voorbeelden

<?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]);

Resultaat

[36,true]

Bronnen