Skip to content

Instantly share code, notes, and snippets.

@frfernandezdev
Created July 3, 2020 19:47
Show Gist options
  • Save frfernandezdev/ec593cda1e685a5d67e65d29666b8208 to your computer and use it in GitHub Desktop.
Save frfernandezdev/ec593cda1e685a5d67e65d29666b8208 to your computer and use it in GitHub Desktop.
<?php
class Event {
private $observers = [];
function __construct(string $event) {
$this->observers[$event] = NULL;
}
function dispatch(string $event, ...$value) {
foreach($this->observers[$event] as $fn) {
if (is_callable($fn)) {
call_user_func_array($fn, $value);
}
}
}
function attach(string $event, $callback) {
$this->observers[$event][] = $callback;
}
}
$event_push = new Event('push');
$event_push->attach('push', function($value) {
echo $value;
});
$event_push->attach('push', function($value) {
echo 'second';
echo $value;
});
$event_push->dispatch('push', 'hello!!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment