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($operation, $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.19, 8.3.0 - 8.3.7
Warning: Undefined variable $operation in /in/7P8Fm on line 123 Fatal error: Uncaught TypeError: Operation::__construct(): Argument #1 ($routine) must be of type Closure, null given, called in /in/7P8Fm on line 123 and defined in /in/7P8Fm:10 Stack trace: #0 /in/7P8Fm(123): Operation->__construct(NULL, Object(Promise)) #1 /in/7P8Fm(130): async(Object(Closure), Object(Queue)) #2 {main} thrown in /in/7P8Fm on line 10
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Warning: Undefined variable $operation in /in/7P8Fm on line 123 Fatal error: Uncaught TypeError: Operation::__construct(): Argument #1 ($routine) must be of type Closure, null given, called in /in/7P8Fm on line 123 and defined in /in/7P8Fm:10 Stack trace: #0 /in/7P8Fm(123): Operation->__construct(NULL, Object(Promise)) #1 /in/7P8Fm(133): async(Object(Closure), Object(Queue)) #2 {main} thrown in /in/7P8Fm on line 10
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: operation in /in/7P8Fm on line 123 Fatal error: Uncaught TypeError: Argument 1 passed to Operation::__construct() must be an instance of Closure, null given, called in /in/7P8Fm on line 123 and defined in /in/7P8Fm:10 Stack trace: #0 /in/7P8Fm(123): Operation->__construct(NULL, Object(Promise)) #1 /in/7P8Fm(133): async(Object(Closure), Object(Queue)) #2 {main} thrown in /in/7P8Fm on line 10
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught TypeError: Argument 1 passed to Operation::__construct() must be an instance of Closure, null given, called in /in/7P8Fm on line 123 and defined in /in/7P8Fm:10 Stack trace: #0 /in/7P8Fm(123): Operation->__construct(NULL, Object(Promise)) #1 /in/7P8Fm(133): async(Object(Closure), Object(Queue)) #2 {main} thrown in /in/7P8Fm on line 10
Process exited with code 255.
Output for 5.6.8 - 5.6.28
Parse error: syntax error, unexpected ':', expecting '{' in /in/7P8Fm on line 121
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/7P8Fm on line 36
Process exited with code 255.

preferences:
155.76 ms | 402 KiB | 228 Q