Skip to content

thomas-bouvier/dependency-injection-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Dependency Injection Container

This Dependency Injection Container manages two kind of data : services and parameters.

Usage

Creating a container is a matter of creating a DIC\Container instance :

use DIC\Container;
$container = new Container();

Defining services

Services are defined by closures that return an instance of an object :

$container->bind('connection', function() {
    return new Connection('database_name', 'root', 'root');
});

The above call is equivalent to the following code, as the Container implements the ArrayAccess interface :

$container['connection'] = function() {
    return new Connection('database_name', 'root', 'root');
};

As objects are only created when you get them, the order of the definitions does not matter.

Retrieving a service is also very easy :

$database_connection = $container->resolve('connection');

The above call is equivalent to the following code :

$database_connection = $container['connection'];

Notice that the closure has access to the current container instance, allowing references to other services or parameters :

$container['foo'] = function() {
    return new Foo();
};
$container['bar'] = function($c) {
    return new Bar($c['foo']);
};

Defining factory services

The container returns the same instance each time you get a service. You can choose to get a new instance for all calls by providing a third parameter factory :

$container->bind('connection', function() {
    return new Connection('database_name', 'root', 'root');
}, true);

This call can also be written by wrapping the closure with the factory() method :

$container['connection'] = $container->factory(function() {
    return new Connection('database_name', 'root', 'root');
});

Now, each call to $container['connection'] returns a new instance of the connection.

Defining parameters

Defining a parameter allows to store global values in the container :

$container['database_name'] = 'DB_PROJECT';

Resolving undefined dependencies

If you access a service which has not previously been defined, the container will attempt to build the dependencies using reflection.

About

A small Dependency Injection Container pattern in PHP

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages