Skip to content
runner framework
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.php_cs
.scrutinizer.yml
.travis.yml
CHANGELOG.md
LICENSE
README.md
composer.json
phpunit.xml

README.md

Runner IoC Container

A small IoC Container for PHP

StyleCI

Installation

$ composer require runner/container

Usage

create an instance of the container, and bind services into the container with a name.

basic binding

use Runner\Container\Container;

$container = new Container();

$container->bind('stack', SplStack::class);

$container->make('stack');

$container->bind(ArrayAccess::class, function () {
    return new ArrayObject();
});

binding implementation

use an interface name as name and bind a concrete implementation to it


$container->bind(ArrayAccess::class, function () {
    return new ArrayObject();
});

$container->make(ArrayAccess::class);

binding singleton

$container->bind(
    'db', 
    function () {
        return new PDO();
    }, 
    true
);

$container->bind();

binding instance

just another way to binding singleton

$pdo = new PDO();

$container->instance('db', $pdo);

alias binding

bind an alias as concrete to a registered service

$container->bind(CacheInterface::class, function () {
    return new FileCache();
});

$container->bind('cache', CacheInterface::class, true);

$container->make('cache');

have fun :)

contextual binding

bind different implementation to classes while doing injecting

$container->bind(CacheInterface::class, function () {
    return new FileCache();
});

$container->bind('redis-cache', function () {
    return new RedisCache();
});

$container->bindContext(
    PageController::class,
    CacheInterface::class,
    function (Container $container) {
        return $container->make('redis-cache');
    }
);

$controller = $container->make(PageController::class);

References

License

MIT

You can’t perform that action at this time.