Dependency Injection and the art of services and containers

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!


Your Guides

Ryan Weaver Leanna Pelham

Questions? Conversation?

  • 2016-02-27 Alexander Lomia

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

  • 2016-02-05 Michal Majer

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

  • 2016-01-12 Putera Kahfi

    thanks for the really usefull tutorial. Good job knpuniversity!

  • 2014-11-17 Ryan Dennler

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

  • 2014-09-17 Joan

    I really needed this, thanks!

  • 2014-07-23 Scott Gutman

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

  • 2014-07-10 Ron Chaplin

    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.

  • 2013-12-25 Gaurav

    Thank you very much for this great stuff

  • 2013-11-06 CCCrazyPannda

    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!

  • 2013-10-03 weaverryan

    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!

  • 2013-10-03 MopedTobias

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

  • 2013-09-22 Dmitry Minkovsky

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

  • 2013-07-02 Paca-vaca

    Hi - good tutorial

  • 2013-05-31 weaverryan

    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!

  • 2013-05-24 Dave

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