The following are a few basic examples for use with the Dependency plugin.
Register a simple service.
Di::add('Example', array(
'className' => 'MyClass',
'classPath' => 'Path/To/My/Class'
));
By convention, service names are CamelCase.
Register an instance as a service.
Di::add('Example', new MyClass());
You can also register an existing instance of a class.
Di::add('Example', $object);
Register an anonymous function which resolves a service.
Di::add('Example', function() {
return new MyClass();
});
You can also use a closure to capture variables used when creating the instance.
Di::add('Example', function() use ($something) {
if ($something) {
return new MyClass();
} else {
return new OtherClass();
}
});
Register a callable function which resolves a service.
Di::add('Example', array($object, 'createExample'));
Di::add('Example', array('SomeClass', 'createExample'));
Di::add('Example', 'SomeClass::createExample');
Lazy load a dependency.
$callback = Di::load('Example');
Lazy loading is important to specify a service without immediately instantiating it.
Register a service with constructor injection.
Di::add('Example', array(
'className' => 'MyClass',
'classPath' => 'Path/To/My/Class',
'params' => array(
'foo' => Di::load('Bar')
)
));
This will instantiate MyClass
passing an instance of the "Bar" service to the $foo constructor argument, for example:
class MyClass {
public function __construct(SomeClass $foo) {
// your code
}
}
Internally this resolves as:
$example = new MyClass(Di::get('Bar'));
Injected services can in turn have their own dependencies.
Register a service with a setter injection.
Di::add('Example', array(
'className' => 'MyClass',
'classPath' => 'Path/To/My/Class',
'setters' => array(
'setFoo' => array(
'foo' => Di::load('Bar')
)
)
));
This will instantiate MyClass
, then call setFoo()
, passing it an instance of the "Bar" service, for example:
$example = new MyClass();
$example->setFoo(Di::get('Bar'));
Injected services can in turn have their own dependencies.
Load a dependency on the fly.
$example = Di::Example();
You can also explicitly get the dependency from the container.
$example = Di::get('Example');
Check if a dependency has been registered.
$exists = Di::has('Example');
If a scope is defined as the second argument of Di::has()
the search will be limited to that scope. Otherwise all scopes will be searched.
Override configuration options at runtime.
$example = Di::Example(array(
'params' => array(
'something' => 'Hello World'
)
));
IMPORTANT: Configuration changes at runtime do not modify the stored instance if one exists.
Configuration options can also be modified previous to instantiation using Di::set()
.
Di::set('Example', array(
'params' => array(
'something' => 'Hello World'
)
));
The values of options, if an array, are merged or overwritten if they're already defined.
You can change the scope of the dependency container at any time.
Di::scope('something');
Dependencies will now be searched for under that scope. The default scope is Di::DEFAULT_SCOPE
.
You can also register a dependency in the Di::GLOBAL_SCOPE
, which will make it always available, irrespective of the current scope. To get the current scope you can call Di::scope()
without an argument.
$scope = Di::scope();
You can also specify a scope when loading a dependency. This will not modify the scope globally, only for the service requested.
$example = Di::Example(array(
'scope' => 'products'
));
If a dependency is requested from a scope where it does not exist an exception is thrown.
Register an observer for an interface or class.
Di::observe('MyInterface', array(
'setters' => array(
'setFactory' => Di::load('Factory')
)
));
Multiple interfaces or classes can also be observed at once.
Di::observe(array(
'MyInterface',
'AbstractClass'
), array(
'setters' => array(
'setFactory' => Di::load('Factory')
)
));