The Immutable Event Dispatcher

Version: 2.4
Edit this page

Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.

Read the updated version of this page for Symfony 6.0 (the current stable version).

The Immutable Event Dispatcher

The ImmutableEventDispatcher is a locked or frozen event dispatcher. The dispatcher cannot register new listeners or subscribers.

The ImmutableEventDispatcher takes another event dispatcher with all the listeners and subscribers. The immutable dispatcher is just a proxy of this original dispatcher.

To use it, first create a normal dispatcher (EventDispatcher or ContainerAwareEventDispatcher) and register some listeners or subscribers:

1
2
3
4
5
6
7
8
use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();
$dispatcher->addListener('foo.action', function ($event) {
    // ...
});

// ...

Now, inject that into an ImmutableEventDispatcher:

1
2
3
4
use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
// ...

$immutableDispatcher = new ImmutableEventDispatcher($dispatcher);

You'll need to use this new dispatcher in your project.

If you are trying to execute one of the methods which modifies the dispatcher (e.g. addListener), a BadMethodCallException is thrown.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.