0

i have a factory for creating an object.

Abbruchprotokoll::class => factory(function(ContainerInterface $c){
    return new Abbruchprotokoll($c->get(Request::class)->getRouterParam('stts-id'), $c->get(MySQL::class));
})

the factory is creating that object with a string and a dependency injection (MySQL Class). In my Abbruchprotokoll::class i have an inject annotation:

/**
 * @Inject
 * @var \Smarty
 */
protected $smarty;

the problem is, that this inject annotation is not resolved. i think this is because i am on FactoryResolver and there is no injectMethodsAndProperties() like in ObjectCreator.

can i use injection annotations with factories in some other way?

JuKe
  • 663
  • 2
  • 7
  • 20

1 Answers1

2

You cannot use annotations with factories, you need to use autowire(Abbruchprotokoll::class) instead. autowire() asks for the class to be autowired, which resolves the annotations.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • but how can i set the first constructor parameter? the first parameter is the ID of an entity and is gets from `getRouterParam('stts-id')` from the Request class – JuKe Jul 23 '18 at 12:46
  • Have a look at the documentation: http://php-di.org/doc/php-definitions.html#autowired-objects You need to use `->constructorParameter()` – Matthieu Napoli Jul 28 '18 at 07:59