Introduction to Dependency Injection

Video loading...

  • 0:15
  • 0:17
    Introduction to Dependency Injection Leanna Pelham
  • 0:30
    Hi guys.
  • 0:31
    In this tutorial, we're going to talk about dependency injection,
  • 0:34
    services, and dependency injection containers
  • 0:37
    by looking at a simple one called Pimple.
  • 0:40
    The great news is that understanding these things isn't hard,
  • 0:43
    but it can dramatically increase the quality
  • 0:45
    and maintainability of the code you write.
  • 0:48
    As always, we'll be coding with a real example.
  • 0:51
    Recently, we noticed that a lot of really nice rich people
  • 0:53
    have been emailing us trying to give away their money.
  • 0:56
    In this tutorial, we're going to create
  • 0:58
    a simple app to help these fine people.
  • 1:00
    We're calling it
  • 1:02
    I've already bootstrapped a small app, which you can download.
  • 1:07
    It uses an SQLite database, so make sure you have it installed.
  • 1:11
    Then chmod 777-- the data directory--
  • 1:14
    and run a script that creates some dummy data for us.
  • 1:19
    The app is really simple.
  • 1:20
    It queries the database then delivers emails
  • 1:23
    to each person using the SMTP mailer class.
  • 1:33
    You could use any mailer library here,
  • 1:36
    and I've made this class fake the sending of emails for simplicity.
  • 1:40
    Instead, it just logs details to a file.
  • 1:43
    Tail the log file, then run the app via php app.php
  • 1:47
    from the command line.
  • 1:56
    You'll see two emails are sent to two lucky people.

Introduction to Dependency Injection


Sharpen your object-oriented skills 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 series, 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 demo application to use a dependency injection container, using a fantastic—but simple—container called Pimple.

In this tutorial, we’ll be coding with a real example where we create a simple app to help people give their money away, we’re calling it We’ve already bootstrapped a small app, which you can download and use if you want to follow along. It uses an SQLite database, so if you don't have that set up, you can grab the database and find installation instructions on the SQLite site. Once you SQLite, you will install the app from the demo code we are providing. We will also be using Composer to make it easier to get Pimple when we need it. If you are not familiar with Composer, you can watch the short tutorial The Wonderful World of Composer to get up and running.

Log in or sign up to download companion files.
Additional resources: 

SQLite site
The Wonderful World of Composer tutorial

To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.