I could make an instance of PDO
and inject it successfully. I defined the PDO::class
directly and injected it in the constructor with __const
-
If you have multiple instances of a class in your app (here you have multiple instances of the PDO
class), then you must configure which one to inject every time.
That means that PDO
cannot be autowired, because PHP-DI cannot decide which instance you want depending on the service/controller/etc.
You need to use configuration (see http://php-di.org/doc/php-definitions.html#autowired-objects) to define which instance (db1
or db2
in your example) to inject for each service.
return [
MyService::class => DI\autowire()
->constructorParameter('pdo', DI\get('db1'))
->constructorParameter('pdo2', DI\get('db2')),
'db1' => function (ContainerInterface $c) {
return new PDO();
},
'db2' => function (ContainerInterface $c) {
return new PDO();
},
];
- 热议问题