Simple service locator implementation capable of using closures to generate instances

category Zend
package Zend_Di

 Methods

Retrieve a registered service

get(string $name, array $params) : mixed

Tests first if a value is registered for the service, and, if so, returns it.

If the value returned is a non-object callback or closure, the return value is retrieved, stored, and returned. Parameters passed to the method are passed to the callback, but only on the first retrieval.

If the service requested matches a method in the method map, the return value of that method is returned. Parameters are passed to the matching method.

Parameters

$name

string

$params

array

Returns

mixed

Register a service with the locator

set(string $name, mixed $service) : \Zend\Di\ServiceLocatorInterface

Parameters

$name

string

$service

mixed

Returns

 Properties

 

Map of service names to methods

$map : array

As an example, you might define a getter method "getFoo", and map it to the service name "foo":

protected $map = array('foo' => 'getFoo');

When encountered, the return value of that method will be used.

Methods mapped in this way may expect a single, array argument, the $params passed to get(), if any.

 

Registered services and cached values

$services : array