Wildcard Listeners

Sometimes you'll want to attach the same listener to all events of a given instance — or potentially, with a shared event collection, all contexts.

Attaching using the wildcard

To attach to all events on a given EventManager instance, you can use the wildcard event, *:

$events = new EventManager();
$events->attach('*', $listener);

Note that if you specify a priority, that priority will be used for this listener for any event triggered.

What the above specifies is that any event triggered by the event manager instance will result in notification of this particular listener.

Attaching using the wildcard via a SharedEventManager

Using the SharedEventManager, you can indicate that you want to attach to all events of a given identifier, a single named event across all identifiers, or all events on all identifiers.

$sharedEvents = new SharedEventManager();

// Attach to all events on the context "foo"
$sharedEvents->attach('foo', '*', $listener);

// Attach to the "foo" event of any context:
$sharedEvents->attach('*', 'foo', $listener);

// Attach to all events on all contexts:
$sharedEvents->attach('*', '*', $listener);

Note that if you specify a priority, that priority will be used for all events specified.

Found a mistake or want to contribute to the documentation? Edit this page on GitHub!