Created
May 30, 2017 23:09
-
-
Save emir/e9adbec372a8ee6ebc873e12d0085410 to your computer and use it in GitHub Desktop.
Visual Debts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface EventInterface { | |
| public function listen(string $name, callable $handler) : void; | |
| public function fire(string $name) : bool; | |
| } | |
| final class Event implements EventInterface { | |
| protected $events = []; | |
| public function listen(string $name, callable $handler) : void | |
| { | |
| $this->events[$name][] = $handler; | |
| } | |
| public function fire(string $name) : bool | |
| { | |
| if (! array_key_exists($name, $this->events)) { | |
| return false; | |
| } | |
| foreach ($this->events[$name] as $event) { | |
| $event(); | |
| } | |
| return true; | |
| } | |
| } | |
| $event = new Event; | |
| $event->listen('subscribed', function () { | |
| var_dump('handling it'); | |
| }); | |
| $event->listen('subscribed', function () { | |
| var_dump('handling it again'); | |
| }); | |
| $event->fire('subscribed'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment