Skip to content

Instantly share code, notes, and snippets.

@windbridges
Last active December 8, 2020 21:29
Show Gist options
  • Save windbridges/4f52860ef930f0feafdbed94a2bf5549 to your computer and use it in GitHub Desktop.
Save windbridges/4f52860ef930f0feafdbed94a2bf5549 to your computer and use it in GitHub Desktop.
<?php
trait WithEvents
{
private $eventHandlers;
public function on(string $event, callable $handler): self
{
$this->eventHandlers[$event][] = $handler;
return $this;
}
public function dispatch(string $event, array $payload = []): self
{
if (isset($this->eventHandlers[$event])) {
foreach ($this->eventHandlers[$event] as $handler) {
call_user_func($handler, $payload);
}
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment