3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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();
based on 7P8Fm
Output for 7.0.0 - 7.2.0
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.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/2qsrV on line 36
Process exited with code 255.
Output for 5.3.18 - 5.3.29
Parse error: syntax error, unexpected '.', expecting '&' or T_VARIABLE in /in/2qsrV on line 36
Process exited with code 255.