getting started  »  services

Services

If you need access to various services throughout the life-cycle of the application, you can register them with the service locator and call upon them later. You can pass an array of services into the constructor, or you can set them individually as needed.

$services = new Pop\Service\Locator([
    'foo' => 'MyApp\SomeService'
]);

$services->set('bar', 'MyApp\SomeService->bar');
$services['baz'] = 'MyApp\SomeService->baz';

Then, you can retrieve a service in a number of ways:

$foo = $services['foo'];
$bar = $services->get('bar');

You can use the isAvailable() method if you'd like to determine if a service is available, but not loaded yet:

if ($services->isAvailable('foo')) {
    $foo = $services['foo'];
} else {
    $services->set('foo', 'MyApp\SomeService');
}

The service locator uses "lazy-loading" to store the service names and their attributes, and doesn't load or create the services until they are actually needed and called from the service locator. The isLoaded() method determines if the service has been set and previously called:

if ($services->isLoaded('foo')) {
    $foo = $services['foo'];
}

You can also remove a service from the service locator if needed:

$services->remove('foo');
unset($services['bar']);

Syntax

You have a couple of different options when setting services. You can pass callable strings or already instantiated instances of objects, although the latter could be potentially less efficient. Valid callable strings are as follows:

  • 'someFunction'
  • 'SomeClass'
  • 'SomeClass->foo'
  • 'SomeClass::bar'

The first example is the name of a callable function. The second callable string example creates a new instance of SomeClass and returns it. The third callable string example creates a new instance of SomeClass, calls the method foo() and returns the value from it. The forth callable string example calls the static method bar() in the class SomeClass and returns the value from it.

Parameters

Additionally, if you need to inject parameters into your service upon calling your service, you can set a service using an array with a call key and a params key like this:

$services = new Pop\Service\Locator([
    'foo' => [
        'call'   => 'MyApp\SomeService->foo',
        'params' => [
            'bar' => 123,
            'baz' => 456
        ]
    ]
]);

In the example above, the service foo is defined by the callable string MyApp\SomeService->foo. When the service foo is retrieved, the locator will create a new instance of MyApp\SomeService, call the method foo while passing the params bar and baz into the method.

Service Container

A static service container class is available if you prefer to track and access your services through it. The first call to create a new service locator object will automatically register it as the 'default' service locator.

Default Service Locator
// Automatically registers it with the default container
$services = new Pop\Service\Locator([
    'foo' => 'MyApp\SomeService'
]);

// Get the service
$foo = Pop\Service\Container::get('default')->get('foo');
Custom Service Locator
// Second boolean parameter prevents it from registering with the default container
$customServices = new Pop\Service\Locator(null, false);

// Register it with the custom container
Pop\Service\Container::set('custom', $customServices);

// Use the custom container to set and get the service
Pop\Service\Container::get('custom')->set('foo', 'MyApp\SomeService');

$foo = Pop\Service\Container::get('custom')->get('foo');