These are just some quick notes really, just for some future reference, mostly concerned with the differences as they pertain to invokables and factories in Zend/ServiceManager.

Invokables

An invokable is simply a class that can be instantiated, it doesn’t normally require any deps and can just be instantiated when needed, like controllers normally have no dependencies that are required during construction nor any deps required after construction and before the object can start work, even if they’re set via setters.

Factories

If your dependency is dependent on other objects that need fetching before the object will work properly, even if these are set via setters rather than during construction and inteligent object, in this case a factory, is the way to go. A factory can be a closure straight in the config (but this stops caching), in the module.php or as concrete classes. These classes are either invokable via __invoke or implement the FactoryInterface.

Closure:

 1 <?php
 2     'service_manager' => array(
 3         'factories' => array(
 4             'something' => function ($sm) {
 5             	$se = $sm->get('somethingelse');
 6             	$s = new Something($se);
 7             	return $s;
 8             },
 9         ),
10     ),

Factory object

 1 <?php
 2     'service_manager' => array(
 3         'factories' => array(
 4             'something' => 'SomethingFactory',
 5         ),
 6     ),
 7 
 8     class SomethingFactory implements FactoryInterface
 9     {
10         public function createService(ServiceLocatorInterface $sm)
11         {
12            	$se = $sm->get('somethingelse');
13            	$s = new Something($se);
14            	return $s;
15         }
16     }

Invokable factory

 1 <?php
 2     'service_manager' => array(
 3         'invokables' => array(
 4             'something' => 'SomethingFactoryInvokable',
 5         ),
 6     ),
 7 
 8 
 9     class Something
10     {
11         public function \__invoke(ServiceLocatorInterface $sm)
12         {
13            	$se = $sm->get('somethingelse');
14            	$s = new Something($se);
15            	return $s;
16         }
17     }

The concrete class way is best when you’re doing some fairly substantial work. You can either implement the FactoryInterface and then the createService method (which takes a servicelocatorinterface object) or just make the class invokable by implementing __invoke (which also takes the sm in). You can then retrieve other objects from the servicemanager (invokables or factories) and then return an instance of some object, of a singluar type per factory, as a result.

And that’s it!