Skip to content
This repository

A lightweight dependency injection container for PHP.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 tests
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
Octocat-spinner-32 container.php
README.md

Fogo

A lightweight dependency injection container for PHP.

Fogo uses PHP's built-in type hinting to inject dependencies directly into your constructors. In the example below, Fogo has wired Ledger to Invoice automatically.

<?php
include 'container.php';

class Ledger { }

class Invoice
{
    private $ledger;

    public function __construct(Ledger $ledger)
    {
        $this->ledger = $ledger;
    }

}

$container = new Container();
$container->add('Ledger');
$container->add('Invoice');

$invoice = $container->getInstance('Invoice');
?>

Fogo also supports interface injection, a staple feature of other containers supporting Inversion of Control (IoC). Example #5 goes into detail about this feature, but the example below touches on it briefly.

<?php
include 'container.php';

interface Connection
{
    public function connect();
}

class MySQLConnection implements Connection
{
    public function connect() { /* ...connect! */ }
}

class Controller
{
    private $connection;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }
}

$container = new Container();
$container->add('Controller');
$container->addImplementation('Connection', 'MySQLConnection');

$controller = $container->getInstance('Controller');
?>

Instance injection is also supported. You may need this if an external resource must be configured prior to injection.

<?php
include 'container.php';

class Logger
{
    private $basePath;
    private $logLevel;

    public function __construct($basePath = '', $logLevel = 'warn')
    {
        $this->basePath = $basePath;
        $this->logLevel = $logLevel;
    }
}

class Controller
{
    private $logger;

    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
    }
}

$logger = new Logger('../logs', 'error');
$container = new Container();
$container->add('Controller');
$container->addInstance($logger);

$controller = $container->getInstance('Controller');

?>
Something went wrong with that request. Please try again.