Zend\EventManager\LazyEventListener extends from
LazyListener, but requires supplying the event name to
which to attach, and optionally the priority, in the definition supplied at
construction. This allows it to be a standalone instance that a listener
aggregate can then query and use to attach to an event manager instance.
As noted in the introduction, the
LazyEventListener is aware of two additional
keys in the definition supplied at instantiation:
- event is the name of the event to which the lazy listener should attach.
- priority can optionally be provided to indicate the priority at which the lazy listener should attach.
As an example, let's assume:
- We have a listener registered in our container with the service name
- we want to use the method
onDispatchwhen listening; further,
- we want to attach it to the event
- at priority 100.
Additionally, we'll assume that we have a container-interop instance in the
$container and an event manager in the variable
You could create the lazy event listener as follows:
use My\Application\Listener; use Zend\EventManager\LazyEventListener; $listener = new LazyEventListener([ 'listener' => Listener::class, 'method' => 'onDispatch', 'event' => 'dispatch', 'priority' => 100, ], $container);
LazyEventListener exposes two methods:
getEvent()returns the event name used.
getPriority($default = 1)returns either the priority passed at instantiation, or, if none was provided, the default passed when invoking the method.
LazyEventListener features are primarily geared towards registering lazy
listeners in aggregates. To that end, you will rarely instantiate or interact
with them directly; instead, you'll leave that to the