Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save wouterj/11367304 to your computer and use it in GitHub Desktop.

Select an option

Save wouterj/11367304 to your computer and use it in GitHub Desktop.
An Event Mediator that fits in a tweet
<?php
class M{function t($̀,$́){foreach(@$this->l[$̀]?:[]as$̃)$̃($́);}function a($̀,$́,$̃){$̂=&$this->l[$̀];while(@$̂[$̃])$̃.='a';$̂[$̃]=$́;krsort($̂,1);}}
<?php
/*
Mediator can work with closures in PHP 5.3+
It can work with any valid callable in PHP 5.4+
*/
$mediator = new M();
/*
M#a() attaches a listener. Arguments:
- event name
- listener
- priority
*/
$mediator->a('hello_message_requested', function ($name) {
echo 'Hello '.$name.'!';
}, 0);
/*
M#t() triggers an event. Arguments:
- event name
- event class (or plain data)
*/
$mediator->t('hello_message_requested', 'world'); // "Hello world!"
<?php
class M
{
function t($e, $a)
{
foreach (@$this->l[$e] ?: [] as $l)
$l($a);
}
function a($e, $l, $p)
{
$s = &$this->l[$e];
while (@$s[$p])
$p .= 'a';
$s[$p] = $l;
krsort($s, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment