Skip to content

Instantly share code, notes, and snippets.

@xsist10
Last active January 21, 2017 17:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsist10/824b559c4effaf43ddb3 to your computer and use it in GitHub Desktop.
Save xsist10/824b559c4effaf43ddb3 to your computer and use it in GitHub Desktop.
An event dispatcher in a tweet
<?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'));
<?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');
@liuggio
Copy link

liuggio commented Oct 22, 2014

maybe you are interested on a something like a framework ... https://github.com/liuggio/sized140

@jm42
Copy link

jm42 commented Oct 31, 2014

Dependency injection container in a tweet ... https://gist.github.com/jm42/3c32dd50bb9d09f57c4a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment