Last active
December 8, 2020 21:29
-
-
Save windbridges/4f52860ef930f0feafdbed94a2bf5549 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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