Dispatcher - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Dispatcher.php
- <?php
- class Dispatcher
- {
- private $events;
- public function addEvent(string $event, Closure $closure): void
- {
- $this->events[$event][] = $closure;
- }
- public function trigger(string $event, $args): void
- {
- foreach ($this->events[$event] as $func) {
- call_user_func($func, $args);
- }
- }
- }
- $dispatcher = new Dispatcher();
- $dispatcher->addEvent('show', function($string) {
- echo 'Show first event ' . $string . PHP_EOL;
- });
- $dispatcher->addEvent('show_another', function($string) {
- echo 'Show second event ' . $string . PHP_EOL;
- });
- $dispatcher->trigger('show', 'Astound');
- $dispatcher->trigger('show_another', 'Astound');