Created
May 1, 2013 20:51
-
-
Save davedevelopment/5498287 to your computer and use it in GitHub Desktop.
Listener removing itself
This file contains 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 | |
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