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 ($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();
based on 3ClGI
Output for 7.1.0 - 7.2.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.20
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.8 - 5.6.28
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.2 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/IKtCY 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/IKtCY on line 36
Process exited with code 255.