This course is archived!
Dependency Injection and the art of services and containers
Unlock next-level object-oriented programming with an insightful tutorial on dependency injection, services, and the Pimple container.
About this course
Get your object-oriented skills sharp by exploring the ideas and reasons behind dependency injection. This simple principle separates developers who write functional code from those that are able to build great, and maintanable applications. In this tutorial, we'll see dependency injection in action, why it's important, and how it relates to services and service-oriented architecture. We'll also refactor our application to use a dependency injection container, using a fantastic - but simple - container called Pimple.
If you're ready to take your object-oriented code to the next level, AND actually be excited about doing it, this tutorial is for you!
Next courses in the Drupal 8: The OO Prerequisites section of the Drupal 8 Track!
15 Comments
Very very nice tutorial on Dependency Injection, knpuniversity is really amazing. Thanks guys.
Great stuff. Clearly you guys spent a ton of time producing something this polished. Thank you!
Hi @MopedTobias!
I've just checked and things seem to be working fine. Are you using a browser plugin to force https? If so, try disabling that - it confuses Amazon's signed URLs (something we'll fix soon). If it's not that, let me know what you're seeing and we'll work it out.
Cheers!
Hi Dave!
Yes, that's a great idea. You're talking specifically about the "Injecting Config & Services and using Interfaces" chapter, where we injected the mailer, then made it more awesome (read: flexible) by creating a MailerInterface. The idea is that we don't really care what your mailer object looks like, as long as it has a sendMessage function on it.
We could (and it would be a great idea) do the same thing with the database connection. We could create a DatabaseInterface which has all the same methods as PHP's PDO object (or at least all of the one's we're going to be using). You can then make a new class that sub-classes PHP's PDO (e.g. MyDatabase) that extends \PDO and implements this interface. The advantage of doing all of this is that your FriendHarvester now expects any object that implements DatabaseInterface. If you wanted to completely replace PDO with something else, you can do that.
Cheers!
Great job guys! I feel like I'm becoming a better devoloper every day on this website. :)
Awesome Series! I have been using DI in Symfony for a while, but this helped me to understand the underlying process so much better! Great Job!
I've recently started using the jms/di-extra-bundle from packagist to allow for dependency injection using annotations. This allows for even cleaner setups I think as there is no requirement for separated config files.
Gotta say that after doing lots, and lots, and lots of research on DI and DI containers, this is the clearest example I've seen. Nicely done!