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(Queue $queue) { $this->queues[] = $queue; } protected function getOperationGenerator() { $queues = []; foreach ($this->queues as $queue) { $queues[] = $queue->getOperationGenerator(); } foreach ($this->queues as $queue) { yield $queue; } } protected function getRunGenerator() { $operation_generator = $this->getOperationGenerator(); foreach ($operation_generator as $operation) { yield $operation; } } public function run() { $generator = $this->getRunGenerator(); while (1) { foreach ($generator as $operation) { $operation->run(); } } } } $main_queue = new Queue; $secondary_queue = new Queue; function async(\Closure $routine, Queue $queue) : Promise { $promise = new Promise(); $operation = new Operation($routine, $promise); $queue[] = $operation; return $promise; } async(function(Promise $promise) { var_dump('Main Fired'); $promise->fulfill(); }, $main_queue)->then(function() { var_dump('Main fulfilled!'); }, function() { var_dump('Main rejected.'); }); async(function(Promise $promise) { var_dump('Secondary Fired'); $promise->fulfill(); }, $secondary_queue)->then(function() { var_dump('Secondary fulfilled!'); }, function() { var_dump('Main rejected.'); }); $queue_handler = new QueueHandler(); $queue_handler->addQueue($main_queue); $queue_handler->addQueue($secondary_queue); $queue->run();
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Cannot use object of type Queue as array in /in/2qsrV:125 Stack trace: #0 /in/2qsrV(130): async(Object(Closure), Object(Queue)) #1 {main} thrown in /in/2qsrV on line 125
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 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
Fatal error: Uncaught Error: Cannot use object of type Queue as array in /in/2qsrV:125 Stack trace: #0 /in/2qsrV(133): async(Object(Closure), Object(Queue)) #1 {main} thrown in /in/2qsrV on line 125
Process exited with code 255.
Output for 5.6.8 - 5.6.28
Parse error: syntax error, unexpected ':', expecting '{' in /in/2qsrV on line 121
Process exited with code 255.
Output for 5.4.8 - 5.4.45, 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/2qsrV on line 36
Process exited with code 255.

preferences:
220.7 ms | 402 KiB | 263 Q