Search code examples
phpdependency-injectionauryn

How to use Auryn for DI in the "right" way?


I'm writing a little application from scratch, and I wanted to use some packages from packagist. For DI I choosed Auryn.

Now, one of the first thing that I learnt about Auryn is that it tries to avoid to be a Dependency Container and it is explicitily wrote in the docs that you should not use the instance of Auryn as a container, passing it through the various classes of your app.

I'm fine with that, but, because I have a "main" class as a wrapper for all of the backbone of the app, I think that I should have in the constructor of this main class only the dependency of an Injector object (That's the main Auryin object), then in the constructor of the class I should wire everything to be ready for DI and reflection.

The other way around is to not use a main class, and just use clean procedural code in my index file, wiring all togheter at the same way with Auryn.

What do you advice is the best way to proceed?


Solution

  • The idea behind dependency injection is to gather all the information how your various classes are "wired up" in one place (your DI container class / DI config file), instead of spreading and duplicating it all over your code. I presume your "Main"-class is only instanciated once in your Index.php, so it makes no noticeable difference whether you pass the Auryn instance to Main's constructor or use Auryn to get an instance of Main.