База знанияИзпитен център
BGБългарски
Избери език13

CKTIS / PHP

Модули и граници

PHP файл може да служи като модул, връщащ извикваема стойност или настройки. require го изпълнява и дава резултата.

Версия: PHP 8.4; repository runtime, production runtime not attested

Синтаксис

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

PHP файл може да служи като модул, връщащ извикваема стойност или настройки. require го изпълнява и дава резултата. Включеният код наследява обхвата на променливите при включването, затова подайте зависимостите като аргументи. Последващо require_once връща 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]

Примери

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

Резултат

[36,true]

Източници