Created
July 3, 2020 19:47
-
-
Save frfernandezdev/ec593cda1e685a5d67e65d29666b8208 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 | |
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