Zend Framework 2 – Invokables vs. Factories vs. Services

zend-logoBeginners in Zend Framework 2 can see using “strange” config keys such as factories, services and invokables in many configuration files. For example:

...
'invokables' => array(
   'Index\Controller\Index' => 'Index\Controller\IndexController',
),
...

or

...
'factories' => array(
      'Book\Model\BookTable' =>  function($sm) {
      $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
      $table = new BookTable($dbAdapter);
      return $table;
   },
),
...

Here are differences between factories, services and invokables in ZF2:

- “invokables” tells to the ServiceManager to instantiate the class when it’s needed.

- “factories” is simillar to the “invokables” but it should be used when you need some additional configuration, as you can see in the examples you mentioned in your question.

- Purpose of the “services” key is to register objects that are already instantiated.

Share This Post

Related Articles

Leave a Reply

 
© 2014 LoneShooter.com. All rights reserved. Site Admin · Entries RSS · Comments RSS
· Designed by Lone Shooter