$container = new Inversion\Container(); $container['foo'] = 'My\Class\Foo'; // ... $foo = $container('foo');
$container->addService(new Service('My\Class\Foo'), 'foo');
The name "foo" comes second, because it can be omitted altogether. More details below.
$foo = $container('foo'); // $foo = $container->get('foo'); // $foo = $container['foo']->get(); // $foo = $container->getService('foo')->get();
However, I recommend using the abbreviated version, although they are all valid.
namespace My\Space; class One {} class Two {} class Bar { public function __construct(One $one, Two $two) { } }
use Inversion\Service; //... $container['one'] = 'My\Space\One'; $container['two'] = 'My\Space\Two'; $container['bar'] = new Service('My\Space\Bar', array($container['one'], $container['two']));
$container['My\Space\One'] = 'My\Space\One'; $container['My\Space\Two'] = 'My\Space\Two'; $container['My\Space\Bar'] = new Service('My\Space\Bar'); // "new Service"
namespace My\Space; class One implements OneInterface {} class Two implements TwoInterface {} class Bar implements BarInterface { public function __construct(OneInterface $one, TwoInterface $two) { } }
$container['My\Space\OneInterface'] = 'My\Space\One'; $container['My\Space\TwoInterface'] = 'My\Space\Two'; $container['My\Space\BarInterface'] = 'My\Space\Bar';
$container[] = 'My\Space\One'; $container[] = 'My\Space\Two'; $container[] = 'My\Space\Bar';
However, you need to understand that in this case the classes will be immediately loaded to get the list of interfaces through reflection. Therefore, it is better to specify the interface name manually.
$container['closure'] = function () use ($container) { return new My\Class(); };
$container['closure'] = function (One $foo, Two $foo) use ($container) { return new My\Class(); };
$container['closure'] = new Closure(function (One $foo, Two $foo) use ($container) { return new My\Class(); }, array($container['one'], $container['two']));
$container['factory'] = new Factory('My\ClassFactory', 'create');
$container['object'] = new My\Class();
$container['object'] = new Object(new My\Class());
$container['prototype'] = new Prototype($object);
$container['data'] = new Data('what you want');
$container['data'] = array(...);
$container['data'] = new Data(array(...));
$container['foo'] = new Service(...); $ref = $container['foo']; // .
$container['My\Class\FooInterface'] = new Service('My\Class\Foo'); $container['foo'] = $container['My\Class\FooInterface']; //... $foo = $container('foo');
//... $container['My\Class\FooInterface'] = new Service('Another\FooImpl'); //... $foo = $container('foo'); // $foo instanseof Another\FooImpl
$container['foo'] = 'My\Class\Foo'; $container['ref'] = $container['foo']; $container['ref2'] = $container['ref']; $container['ref3'] = $container['ref2']; //... $foo = $container('ref3'); // $foo instanseof My\Class\Foo $name = $container->getRealName('ref3'); // $name == 'foo'
$container['My\Class\FooInterface'] = 'My\Class\Foo'; //... $container['My\Class\FooInterface'] = function (FooInterface $foo) { $foo->extendSome(...); return $foo; };
$container['My\Class\FooInterface'] = 'My\Class\Foo'; //... $container->extend('My\Class\FooInterface', function (FooInterface $foo) { return new FooDecorator($foo); });
$container = Inversion\Container::getInstanse();
Source: https://habr.com/ru/post/169187/
All Articles