nish / simple-container
Simple Auto Wiring Container
v1.1.0
2024-11-01 07:57 UTC
Requires
- php: >=7.1
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
Simple container of auto wiring with constructor.
<?php use Nish\Container\Container; require_once 'vendor/autoload.php'; $c = Container::getInstance(); $obj = $c->get(stdClass::class); $c->call(Foo::class, 'staticMethod'); $foo = new Foo(); $c->call($foo, 'method'); $c->setFactory('MyClass', function($c){ return new MyClass('custom param'); }); $c->setFactory(MyClass::class, function($c){ $obj = new MyClass('custom param'); $c->set(MyClass::class, $obj); // singleton return $obj; }); // set arguments namespace MyProject; class Db { private $dsn; public function __construct(string $dsn){ $this->dsn = $dsn; } // ... } $c->set('MyProject\\Db#__construct.dsn', 'mysql://dbname...'); $db = $c->get(MyProject\\Db::class);
@see https://github.com/nishphp/simple-container/blob/master/test/ContainerTest.php