This course is archived!

> Drupal 8 >

Course Overview

Dependency Injection and the art of services and containers

  • 2970 students
  • EN Captions
  • EN Script
  • Certificate of Completion

Your Guides

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

Sort By
Login or Register to join the conversation
Default user avatar CCCrazyPannda 5 years ago

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!

8 | Reply |
Default user avatar Ryan Dennler 5 years ago

Very very nice tutorial on Dependency Injection, knpuniversity is really amazing. Thanks guys.

1 | Reply |

Clear, concise and chock full of nuts. I am definately purchasing more

1 | Reply |
Default user avatar Dmitry Minkovsky 5 years ago

Great stuff. Clearly you guys spent a ton of time producing something this polished. Thank you!

| Reply |
Default user avatar MopedTobias 5 years ago

I cannot watch the videos neither download them. What's up?

| Reply |

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!

| Reply |

I really needed this, thanks!

| Reply |
Default user avatar Putera Kahfi 5 years ago

thanks for the really usefull tutorial. Good job knpuniversity!

| Reply |
Default user avatar Gaurav 5 years ago

Thank you very much for this great stuff

| Reply |

Shouldn't you also create a DbInterface to be more flexible?

| Reply |

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!

1 | Reply |
Default user avatar Paca-vaca 5 years ago

Hi - good tutorial

| Reply |
Default user avatar Michal Majer 5 years ago

Great job guys! I feel like I'm becoming a better devoloper every day on this website. :)

| Reply |
Default user avatar Ron Chaplin 5 years ago

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.

| Reply |
Default user avatar Alexander Lomia 5 years ago

These screencasts are pure gold, keep up the good work!

| Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!