# DI Container

Wolff\Core\Container

Wolff includes a container which can be used to perform dependency injection on objects.

This is a great and clean way of defining new classes, since its dependencies are 'injected' into them and the instantiation only must be defined one time.

The container's methods are static, you can add and get elements anywhere inside Wolff, but it's recommended to add elements in the system/web.php file and get them when neccesary in your controllers.

General methods

Add

add(string $class, $val)

Adds a new class to the container.

The first parameter is the class name.

The second parameter is the class value. So it can be either a new instantiation of the class, or a function returning the class.

Container::add('user', function() {
    return new \App\User();
});
Container::add('user', new \App\User());

Both examples will work exactly the same.

If a class with the same name already exists in the container, it will be overwritten.

Add singleton

singleton(string $class, $val)

Adds a new singleton class to the container.

When adding a singleton and calling it throught the container, the container will make only one instance no matter how many time it's being called. Just like a singleton or a 'static class'.

Container::singleton('user', function() {
    return new \App\User();
});

Get

get(string $key[, array $args])

Returns the specified class instance.

Container::get('user');

An optional second parameter can be pass to the method which must be an array with the parameters for the constructor of the class.

Defining the class:

Container::add('user', function($params) {
    return new \App\User($params);
});

Getting the instance:

$user = Container::get('user', [
    'John doe', 22
]);

That would be the equivalent to this:

$user = new \App\User('John doe', 22);

Has

has(string $key)

Returns true if the given class name exists, false otherwise.

Container::has('user');

That will return true only if the user class has been added to the container.

Documentation made with
Something is wrong in here? Make a issue/pull request on the repo.