runner / container
container for runner framework
v1.0.3
2020-05-15 15:29 UTC
Requires
- php: >=7.1
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.3
This package is auto-updated.
Last update: 2024-10-16 01:42:33 UTC
README
A small IoC Container for PHP
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