When I read the Zend\MVC\Service\ServiceManagerConfig.php in ZF2. I get a problem. Here is the code:
$serviceManager->addInitializer(function ($instance) use ($serviceManager) {
if ($instance instanceof EventManagerAwareInterface
&& !$instance->getEventManager() instanceof EventManagerInterface
) {
$instance->setEventManager($serviceManager->get('EventManager'));
}
});
I know that there is a closure in the method “addInitializer”.But what makes me confused is the parameter “$instance”. It’s a parameter but where is the real value from ? var_dump() of $instance and find that it’s a object,that’s “Zend\ModuleManager\Listener\ServiceListener” ; Where does it come in ?
When you do
$sm->get('something');object will be created using defined factories and then passed to initializers.$instanceparameter is that object.You can see that here