Mediator pattern passing data php -


class mediator {     protected $events = array();     public function attach($eventname, $callback) {         if (!isset($this->events[$eventname])) {             $this->events[$eventname] = array();         }         $this->events[$eventname][] = $callback;     }     public function trigger($eventname, $data = null) {         foreach ($this->events[$eventname] $callback) {             $callback($eventname, $data);         }     } } $mediator = new mediator; $mediator->attach('stop', function() { echo "stopping"; }); $mediator->attach('stop', function() { echo "stopped"; }); $mediator->trigger('stop'); // prints "stoppingstopped" 

i can't figure out how can pass data pattern, i.e. want pass database object, ends this.

$mediator->attach('test', function($test) { echo $test; }); $mediator->trigger('test', '123'); 

it prints out "test", not 123.

all need replace :

$callback($eventname, $data); 

with

$callback($data); 

see live demo


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -