Skip to content

Instantly share code, notes, and snippets.

@emir
Created May 30, 2017 23:09
Show Gist options
  • Select an option

  • Save emir/e9adbec372a8ee6ebc873e12d0085410 to your computer and use it in GitHub Desktop.

Select an option

Save emir/e9adbec372a8ee6ebc873e12d0085410 to your computer and use it in GitHub Desktop.
Visual Debts
<?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