r/PHPhelp • • May 30 '18

PHP Dependency Injection tutorials?

I need a tutorial on using Dependency Injection/IOC in PHP. We are going to build an API and want the output formatters to be "pluggable" meaning we may use DI to accomplish this. I don't know how to do DI in PHP and need some brushing up on it.

9 Upvotes

6 comments sorted by

3

u/pernicat May 30 '18

If you want to understand the basic concepts behind DI I would recommend checking out PHP The Right Way.

If you want to play around with a simple barebones DI container try Pimple. It does not have fancy features (like autowiring) but it is super simple to learn and can help you understand some of the concepts. Most of the more advance DI containers seem to have complex configuration syntax that gets in the way of understanding how it all fits together.

1

u/MrBaseball77 May 30 '18

PHP-DI looks fairly interesting, more like the way I have done it in C#.

2

u/devhourstudio May 30 '18

I use PHP-DI on a large scale project, it works well

1

u/doitstuart May 30 '18

The oft-cited no-framework tutorial has a good section on wiring up DI at the nuts and bolts level:

https://github.com/PatrickLouys/no-framework-tutorial/blob/master/08-dependency-injector.md

0

u/devhourstudio May 30 '18

Laracasts have some good tutorials. https://laracasts.com/series/php-for-beginners/episodes/22

https://laracasts.com/series/solid-principles-in-php/episodes/5

https://laracasts.com/lessons/dependency-injection-containers

I think you are mainly referring to dependency inversion. Are you planning on using a framework like Laravel?