The code from "PHP Bits: Visual Debt" https://laracasts.com/series/php-bits/episodes/1
<?php
$event = new Event;
$event->listen('subscribed', function () {
var_dump('handling it');
});
$event->listen('subscribed', function () {
var_dump('handling it again');
});
$event->fire('subscribed');
<?php
interface EventInterface {
public function listen(string $name, callable $handler) : void;
public function fire(string $name) : bool;
}
<?php
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;
}
}