Zend\EventManager\LazyListener provides a callable wrapper around fetching a listener from a container and invoking it.


To create a LazyListener instance, you must pass to its constructor:

As an example, let's assume:

I might then create and attach my lazy listener as follows:

use My\Application\Listener;
use Zend\EventManager\LazyListener;

$events->attach('foo', new LazyListener([
    'listener' => Listener::class,
    'method'   => 'onDispatch',
], $container));

LazyListener implements the method __invoke(), allowing you to attach it directly as a callable listener!

Internally, it will do essentially the following:

$listener = $container->get($this->listener);
$method   = $this->method;
return $listener->{$method}($event);