dcp/di

Provides a minimalistic dependency injection container.

dev-master 2015-12-07 00:52 UTC

This package is not auto-updated.

Last update: 2024-04-23 02:41:22 UTC


README

About

Provides a minimalistic dependency injection container.

Example

use DCP\Di\Container;
use DCP\Di\Service\Reference;

interface MailerInterface {}

class Mailer implements MailerInterface
{
    protected $transport;

    public function __construct(MailerTransportInterface $transport)
    {
        $this->transport = $transport;
    }
}

interface MailerTransportInterface {}

class MailerTransport implements MailerTransportInterface
{
    protected $host;
    protected $port;

    public function __construct($host, $port)
    {
        $this->host = $host;
        $this->port = $port;
    }
}

$di = new Container();

$di
    ->register(MailerInterface::class, 'mailer')
    ->toClass(Mailer::class)
;

$di
    ->register(MailerTransportInterface::class)
    ->toInstance(new Reference(MailerTransport::class))
;

$di
    ->register(MailerTransport::class)
    ->addArguments([
        'host' => 'localhost',
        'port' => 25
    ])
;

var_dump($di->get('mailer'));