Example
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
$dispatcher = new EventDispatcher();
// you can attach event subscribers, which allow a single object to subscribe
// to many events at once
$dispatcher->addSubscriber(new class implements EventSubscriberInterface {
public static function getSubscribedEvents()
{
// here we subscribe our class methods to listen to various events
return [
// when anything fires a "an.event.occurred" call "onEventOccurred"
'an.event.occurred' => 'onEventOccurred',
// an array of listeners subscribes multiple methods to one event
'another.event.happened' => ['whenAnotherHappened', 'sendEmail'],
];
}
function onEventOccurred(Event $event) {
// process $event
}
function whenAnotherHappened(Event $event) {
// process $event
}
function sendEmail(Event $event) {
// process $event
}
});