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($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 ($queues as $queue) { yield from $queue; } } protected function getRunGenerator() { $operation_generator = $this->getOperationGenerator(); foreach ($operation_generator as $operation) { yield $operation; } } public function run() { $generator = $this->getRunGenerator(); foreach ($generator as $operation) { $operation->run(); } } } function addOperation(\Closure $routine, Queue $queue) { $promise = new Promise(); $operation = new Operation($routine, $promise); $queue->append($operation); return $promise; } $queue_handler = new QueueHandler(); $queue_handler->addQueue('main', new Queue()); $queue_handler->addQueue('secondary', new Queue()); $runtime = function(Promise $promise) use ($queue_handler) { echo "first"; $request_promise = addOperation(function() { echo 'last'; }, $queue_handler->getQueue('secondary')); echo "second"; }; addOperation($runtime, $queue_handler->getQueue('main')); $queue_handler->run();
based on gtNEr
Output for 7.0.0 - 7.2.0
firstsecondlast
Output for 5.6.8 - 5.6.28
Parse error: syntax error, unexpected '$queue' (T_VARIABLE) in /in/WCQA1 on line 97
Process exited with code 255.
Output for 5.4.2 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/WCQA1 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/WCQA1 on line 36
Process exited with code 255.