Last active
December 11, 2015 08:58
-
-
Save ajcrites/4576528 to your computer and use it in GitHub Desktop.
Passing defined methods/functions (sort of)
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 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