Skip to content

Instantly share code, notes, and snippets.

@davedevelopment
Created May 1, 2013 20:51
Show Gist options
  • Save davedevelopment/5498287 to your computer and use it in GitHub Desktop.
Save davedevelopment/5498287 to your computer and use it in GitHub Desktop.
Listener removing itself
<?php
require "vendor/autoload.php";
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
$dispatcher = new EventDispatcher;
$eventName = 'dave';
$listener = function() use ($eventName, &$listener, $dispatcher) {
echo "doing things\n";
$dispatcher->removeListener($eventName, $listener);
};
$dispatcher->addListener($eventName, $listener);
$dispatcher->addListener($eventName, function() { echo "other things\n"; });
$dispatcher->dispatch($eventName, new Event());
$dispatcher->dispatch($eventName, new Event());
$dispatcher->dispatch($eventName, new Event());
$dispatcher->dispatch($eventName, new Event());
echo 'done';
// doing things
// other things
// other things
// other things
// other things
// done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment