Skip to content

Instantly share code, notes, and snippets.

@fillano fillano/test001.php
Last active Sep 11, 2018

Embed
What would you like to do?
a simple event facilities test.
<?php
class Event {
private static $events = [];
public static function Regist($eventName, Callable $callback) {
if(!is_callable($callback)) return false;
if(!array_key_exists($eventName, self::$events)) {
self::$events[$eventName] = [];
}
self::$events[$eventName][] = $callback;
return true;
}
public static function Trigger($eventName, $eventData) {
if(!array_key_exists($eventName, self::$events)) return false;
foreach(self::$events[$eventName] as $c) {
call_user_func($c, $eventData);
}
}
public static function Unregist($eventName, Callable $callback) {
$key = array_search($callback, self::$events[$eventName]);
if($key != false) {
array_splice(self::$events[$eventName], $key, 1);
}
}
}
class Test001 {
private $name;
function __construct($name) {
$this->name = $name;
}
public function greeting($data) {
echo $data . " " . $this->name . "<br>\n";
}
}
$a = new Test001("Fillano");
$b = new Test001("Hildegard");
Event::Regist("event1", [$a, "greeting"]);
Event::Regist("event1", [$b, "greeting"]);
Event::Trigger("event1", "Hello World");
Event::Unregist("event1", [$b, "greeting"]);
Event::Trigger("event1", "Hello Another World");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.