3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Operation { protected $routine; protected $promise; public function __construct(\Closure $routine, $promise) { $this->routine = $routine; $this->promise = $promise; } public function run() { $routine = $this->routine(); $routine($this->promise); } public function getPromise() { return $this->promise; } } class Promise { protected $successClosure; protected $failClosure; public function fulfill(...$args) { $closure = $this->successClosure; $closure(...$args); } public function reject(...$args) { $closure = $this->failClosure; $closure(...$args); } public function then(\Closure $success, \Closure $fail) { $this->successClosure = $success; $this->failClosure = $fail; } } class Queue { protected $queue = []; public function append(Operation $operation) { $this->queue[] = $operation; } public function getOperationGenerator() { foreach ($this->queue as $operation) { yield $operation; } } } class QueueHandler { protected $queues = []; public function addQueue($handle, Queue $queue) { $this->queues[$handle] = $queue; } public function getQueue($handle, $defualt=null) { return isset($this->queues[$handle]) ? $this->queues[$handle] : $default; } protected function getOperationGenerator() { $queues = []; foreach ($this->queues as $queue) { $queues[] = $queue->getOperationGenerator(); } foreach ($this->queues as $queue) { foreach ($queue as $operation) { yield $operation; } } } protected function getRunGenerator() { $operation_generator = $this->getOperationGenerator(); foreach ($operation_generator as $operation) { yield $operation; } } public function run() { $generator = $this->getRunGenerator(); foreach ($generator as $operation) { var_dump($operation); $operation->run(); } } } function addOperation(\Closure $routine, Queue $queue) { $promise = new Promise(); $operation = new Operation($routine, $promise); $queue->append($operation); return $promise; } addOperation(); $queue_handler = new QueueHandler(); $queue_handler->addQueue('main', new Queue()); $queue_handler->addQueue('secondary', new Queue()); $runtime = function(Promise $promise) use ($queue_handler) { $handle_request = function(Promise $promise) { var_dump('handled request'); $promise->fulfill(); }; addOperation($handle_request, $queue_handler->getQueue('secondary')); }; addOperation($runtime, $queue_handler->getQueue('main')); $queue_handler->run();
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught ArgumentCountError: Too few arguments to function addOperation(), 0 passed in /in/IKtCY on line 131 and exactly 2 expected in /in/IKtCY:123 Stack trace: #0 /in/IKtCY(131): addOperation() #1 {main} thrown in /in/IKtCY on line 123
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught ArgumentCountError: Too few arguments to function addOperation(), 0 passed in /in/IKtCY on line 131 and exactly 2 expected in /in/IKtCY:123 Stack trace: #0 /in/IKtCY(131): addOperation() #1 {main} thrown in /in/IKtCY on line 123
Process exited with code 255.
Output for 7.0.0 - 7.0.33
Fatal error: Uncaught TypeError: Argument 1 passed to addOperation() must be an instance of Closure, none given, called in /in/IKtCY on line 131 and defined in /in/IKtCY:123 Stack trace: #0 /in/IKtCY(131): addOperation() #1 {main} thrown in /in/IKtCY on line 123
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Catchable fatal error: Argument 1 passed to addOperation() must be an instance of Closure, none given, called in /in/IKtCY on line 131 and defined in /in/IKtCY on line 123
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/IKtCY on line 36
Process exited with code 255.

preferences:
230.53 ms | 402 KiB | 373 Q