The ServiceManager
is a Service Locator implementation. A Service Locator is a well-known object in which you may register objects and later retrieve them. The implementation within Zend Framework provides the following features:
- Service registration. You can register an object under a given name ($services->setService('foo', $object)).
- Lazy-loaded service objects. You can tell the manager what class to instantiate on first request ($services->setInvokableClass('foo', 'FullyQualifiedClassname')).
- Service factories. Instead of an actual object instance or a class name, you can tell the manager to invoke the provided factory in order to get the object instance. Factories may be either any PHP callable, an object implementing
Zend\ServiceManager\FactoryInterface
, or the name of a class implementing that interface. - Service aliasing. You can tell the manager that when a particular name is requested, use the provided name instead. You can alias to a known service, a lazy-loaded service, a factory, or even other aliases.
- Abstract factories. An abstract factory can be considered a "fallback" -- if the service does not exist in the manager, it will then pass it to any abstract factories attached to it until one of them is able to return an object.
- Initializers. You may want certain injection points always populated -- as an example, any object you load via the service manager that implements
Zend\EventManager\EventManagerAware
should likely receive anEventManager
instance. Initializers are PHP callbacks or classes implementingZend\ServiceManager\InitializerInterface
; they receive the new instance, and can then manipulate it.
In addition to the above, the ServiceManager
also provides optional ties to Zend\Di
, allowing Di
to act as an initializer or an abstract factory for the manager.
Your typical interaction with a ServiceManager
, however, will be via two methods:
has($name)
, for testing whether theServiceManager
has a named service;get($name)
, for retrieving a service by the given name.