A PHP Injection liblary
A PHP Injection Liblary., (*1)
composer require renshan/tc
namespace Foo; class Foo { public function foo() { // Do anything here } }
use Tc\Container; $container = new Container; $defKey = 'service.foo'; $defMap = 'Foo\Foo'; $container->register($defKey, $defMap);
namespace Bar; use Foo\Foo; class Bar { /** * This class need a parameter whic a instance of Foo, and a parameter $bar */ public function constructor(Foo $foo, $bar) { } public function bar() { // Do anything here } }
and you can define a service like this:, (*2)
$container = new Container; $defKey = 'service.bar'; $defMap = 'Bar\Bar'; $params = ['@service.foo', 'bar']; // Be sure the position of parameters $container->register($defKey, $defMap, $params);
finally, you can use your services now:, (*3)
$foo = $container->get('service.foo'); $bar = $container->get('service.bar');
and you can get a raw service:, (*4)
$contnainer->raw('service.foo');
By default, a service will generate one and only a instance of the map class., (*5)