Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Last active December 11, 2015 08:58
Show Gist options
  • Save ajcrites/4576528 to your computer and use it in GitHub Desktop.
Save ajcrites/4576528 to your computer and use it in GitHub Desktop.
Passing defined methods/functions (sort of)
<?php
class Mediator {
private $events;
public function attach($event, Callable $callback) {
if (!isset($this->events[$event])) {
$this->events[$events] = array();
}
$this->events[$event][] = $callback;
}
public function trigger($event) {
if (isset($this->events[$event])) {
foreach ($this->events[$event] as $func) {
$func();
}
}
}
}
class DefinesSomething {
public function attachTo(Mediator $m) {
$m->attach('event', function () use ($this) {
$this->event();
});
}
public function event() {
echo "Event triggered\n";
}
}
$ds = new DefinesSomething;
$m = new Mediator;
$ds->attachTo($m);
$m->trigger('event');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment