1
votes

I'm trying to get a Zend\EventManager\EventManager from a Di

$di     = new Di();
$di->configure(new Config(array(/*etc*/));
$events = $di->get('Zend\EventManager\EventManager');

However I get this error

Fatal error: Uncaught exception 'Zend\Di\Exception\RuntimeException' with message 'Invalid instantiator of type "NULL" for "Zend\EventManager\SharedEventManagerInterface".'

I believe it is because EventManagerInterface extends SharedEventManagerInterface?

I have tried

'preferences'   => array(
    'Zend\EventManager\SharedEventManagerInterface' 
        => 'Zend\EventManager\EventManager',
),

and also defining an instantiator

'Zend\EventManager\SharedEventManagerInterface' => array(
    'instantiator' => 'Zend\EventManager\EventManager::__construct'
),

What should be the config to over come this error?

1

1 Answers

0
votes

Zend\EventManager\EventManager is not a valid Zend\EventManager\SharedEventManagerInterface: use a Zend\EventManager\SharedEventManager instead.

Use following configuration if you want to inject a shared event manager:

'di' => array( 
    'instance' => array(
        'preference' => array(
            'Zend\EventManager\SharedEventManagerInterface' 
                => 'Zend\EventManager\SharedEventManager',
        ),
    ),
),

That will basically tell Zend\Di to use a Zend\EventManager\SharedEventManager each time a generic Zend\EventManager\SharedEventManagerInterface is requested.

I have a working example in this blogpost.