3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class EventBus { public function addEventListener(Listener $listener) { $this->listeners[] = $listener; } public function dispatch(Event ...$events) { foreach($events as $event){ foreach($this->listeners as $listener){ $listener->when($event); } } } } abstract class Listener { public function __construct(EventBus $bus) { $bus->addEventListener($this); } public function when(Event $event) { $method = explode('\\', get_class($event)); $method = 'on' . end($method); if (method_exists($this, $method)) { $this->$method($event); } } } class SendEmailListener extends Listener { public function onMemberRegistered(Event $event) { // send email } } $eventBus = new EventBus; new SendEmailListener($eventBus);
based on A91fe
Output for 5.6.0 - 5.6.30, 7.1.0 - 7.2.4
Output for hhvm-3.22.0
Fatal error: Uncaught Error: Parameter $events is variadic and has a type constraint (Event); variadic params with type constraints are not supported in non-Hack files in /in/LTG2d:-1 Stack trace: #0 {main}
Process exited with code 255.
Output for hhvm-3.18.5 - 3.21.3
Fatal error: Uncaught Error: Parameter $events is variadic and has a type constraint (Event); variadic params with type constraints are not supported in non-Hack files in /in/LTG2d:10 Stack trace: #0 {main}
Process exited with code 255.