2.1. Overview

Capsule is a PSR-11 (2.0) compliant autowiring dependency injection container with object-oriented configuration of constructor arguments and initialization methods, along with lazy resolution of arguments from various sources. Intended primarily for object entries, Capsule makes allowance for storing value entries as well.

Given a hypothetical DataSource class ...

class DataSource
    public function __construct(PDO $pdo)
        $this->pdo = $pdo;

    /* ... methods using $this->pdo ... */

... the following Capsule Container provides a shared instance of that DataSource using a shared instance of PDO:

use Capsule\Di\Container;
use Capsule\Di\Definitions;

$def = new Definitions();


$container = new Container($def);

$dataSource = $container->get(DataSource::CLASS);

Capsule reflects on the DataSource class and sees that it needs a PDO instance. In turn, Capsule examines the PDO class definition, and creates a PDO instance using the PDO_* environment variables. Having done so, Capsule then uses that PDO instance to create the DataSource, and returns it.

Although Capsule can inject object arguments implicitly based on the parameter type, it will not do so for scalar, array, resource, etc. types, nor will it do so for union typed parameters. Arguments for those types of parameters must be explicitly specified in the definition.