Last active
January 21, 2017 17:28
-
-
Save xsist10/824b559c4effaf43ddb3 to your computer and use it in GitHub Desktop.
An event dispatcher in a tweet
This file contains hidden or 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 | |
// Version 1 | |
// Minified | |
// class Dispatch{function add($e,$l){$this->l[$e][]=$l;}function trigger($e,$d){foreach ($this->l[$e] as $l)call_user_func_array($l, $d);}} | |
class Dispatch{ | |
function add($e, $l) { | |
$this->l[$e][] = $l; | |
} | |
function trigger($e, $d) { | |
foreach ($this->l[$e] as $l) { | |
call_user_func_array($l, $d); | |
} | |
} | |
} | |
class Greeter { | |
public function greet($name) { | |
echo "Hello $name\n"; | |
} | |
} | |
$dispatch = new Dispatch(); | |
$dispatch->add('greet', array(new Greeter, 'greet')); | |
$dispatch->trigger('greet', array('Bob')); |
This file contains hidden or 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 | |
// Version 2 | |
// Minified | |
// class Dispatch{function add($e,$l){$this->l[$e][]=$l;}function trigger($e,$d){foreach($this->l[$e] as$l)$l($d);}} | |
class Dispatch { | |
function add($e, $l) { $this->l[$e][]=$l; } | |
function trigger($e, $d) { foreach ($this->l[$e] as $l) $l($d); } | |
} | |
$dispatch = new Dispatch(); | |
$dispatch->add('greet', function ($name) { | |
echo "Hello $name\n"; | |
}); | |
$dispatch->trigger('greet', 'Bob'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependency injection container in a tweet ... https://gist.github.com/jm42/3c32dd50bb9d09f57c4a