PHP Inversion of Control (IoC) Library
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/IoC * php 5.4.0 required Feb 27, 2015
tests * php 5.4.0 required Feb 27, 2015
.gitattributes init Feb 18, 2014
.gitignore init Feb 18, 2014
phpunit.xml.dist init Feb 18, 2014

PHP Inversion of Control(IoC) Library

Very simple tool for managing class dependencies. Dependency injection is a method of removing hard-coded class dependencies. Instead, the dependencies are injected at run-time, allowing for greater flexibility as dependency implementations may be swapped easily.

Here is a simple example that shows how to register services and parameters:

use IoC\Container;

$c = new Container();

$c->setParameter('cache', array(
    'host' => '',
    'port' => 11211,

$c->singleton('cache', function (Container $c) {
    $cache = new Memcached();
    return $cache;

// single instance of Memcached
$value = $c->make('cache');

Resolving a class dependencies:

use IoC\Container;

$c = new Container();
$c->singleton('cache', 'MemcachedCache');

class MemcachedCache extends Cache
    public function __construct(Memcached $cache)
        $this->cache = $cache;

$cache = $c->make('cache');

Resolving a simple class:

use IoC\Container;

$c = new Container();

class FooBar
    public function __construct(Container $c, Baz $baz)
        $this->container = $c;
        $this->baz = $baz;

$fooBar = $c->make('FooBar');

Binding an interface to an implementation:

use IoC\Container;

$c = new Container();
$c->bind('UserRepositoryInterface', 'DbUserRepository');

class UserController extends BaseController {

    public function __construct(UserRepositoryInterface $users)
        $this->users = $users;




The recommended way to install php-ioc is through Composer. Just create a composer.json file and run the php composer.phar install command to install it:

    "require": {
        "itlessons/php-ioc": "*"

Alternatively, you can download the file and extract it.


You can run the unit tests with the following command:

$ cd path/to/php-ioc/
$ composer.phar install
$ phpunit


[Принцип Inversion of Control (IoC) в вашем php проекте] (