0

I have been using PHP-DI and i wanted to enquire if there is a way to inject variables into various class methods automatically ie using autowiring.

Thanks

Stanley Ngumo
  • 4,089
  • 8
  • 44
  • 64

1 Answers1

0

You could use annotations, but that means you would have to annotate every method so it might not be as "automatic" as you hope.

You could also create a definition using wildcards:

return [
    'MyProject\Controller\*' => DI\object()
        ->method('setSomething', DI\get('Foo')),
];

But honestly I would recommend not doing that and rather write injections manually. This is more explicit and easier to understand when re-reading the code 6 months later, or when a co-worker joins the project.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261