2017 © Pedro Peláez
 

library php-ioc

PHP Inversion of Control(IoC) Library

image

itlessons/php-ioc

PHP Inversion of Control(IoC) Library

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

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., (*1)

Here is a simple example that shows how to register services and parameters:, (*2)

use IoC\Container;

$c = new Container();

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

$c->singleton('cache', function (Container $c) {
    $cache = new Memcached();
    $cache->addServer(
        $c->getParameter('cache.host'),
        $c->getParameter('cache.port')
    );
    return $cache;
});

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

Resolving a class dependencies:, (*3)

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:, (*4)

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:, (*5)

use IoC\Container;

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

class UserController extends BaseController {

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

}

$c->make('UserController');

Installation

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:, (*6)

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

Alternatively, you can download the php-ioc.zip file and extract it., (*7)

Resources

You can run the unit tests with the following command:, (*8)

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

[Принцип Inversion of Control (IoC) в вашем php проекте] (http://www.itlessons.info/php/inversion-of-control/), (*9)

The Versions

27/02 2015

dev-master

9999999-dev https://github.com/itlessons/php-ioc

PHP Inversion of Control(IoC) Library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

container ioc dependency

27/02 2015

0.0.1

0.0.1.0 https://github.com/itlessons/php-ioc

PHP Inversion of Control(IoC) Library

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

container ioc dependency