Services and Dependency Injection

Video loading...

  • 0:00
    [MUSIC PLAYING]
  • 0:00
    DEPENDENCY INJECTION AND THE ART OF SERVICES AND CONTAINERS
  • 0:03
    Services and Dependency Injection with Leanna Pelham
  • 0:09
    Our app is small now, but as it grows the app.php
  • 0:12
    file will get harder and harder to read.
  • 0:15
    The best way to fix this is to separate each different chunk
  • 0:18
    of functionality into different PHP classes and methods.
  • 0:22
    Each of these classes is called a service.
  • 0:25
    And the whole idea is sometimes called
  • 0:26
    Service-oriented Architecture.
  • 0:29
    Create a new file in source DiDemo called FriendHarvester.php, which
  • 0:34
    will be responsible for sending the email
  • 0:36
    to every lucky person in the database.
  • 0:39
    Add the namespace that it follows the directory structure
  • 0:42
    and give it an emailFriends() method.
  • 0:52
    Copy in all of our logic into this new method.
  • 1:03
    And just like that, you've created your first service.
  • 1:06
    Roughly speaking, a service is any PHP class that performs an action.
  • 1:11
    Since this sends emails to our new soon-to-be rich friends,
  • 1:14
    it's a service.
  • 1:21
    The app.php code gets pretty simple now.
  • 1:23
    Just instantiate the FriendHarvester and call the method.
  • 1:41
    But when we try it, we get a huge error.
  • 1:44
    Once we remove the code, we don't have access to the PDO object
  • 1:48
    anymore.
  • 1:49
    So how can we get it?
  • 1:50
    This is our first important crossroads.
  • 1:53
    There are few cheating ways to do this,
  • 1:55
    like using the dreaded global keyword.
  • 2:01
    Don't use this.
  • 2:05
    You could also make the PDO variable available statically,
  • 2:09
    by creating some class and then referencing it.
  • 2:28
    The problem with both approaches is that our FriendHarvester has
  • 2:31
    to assume that the PDO variable has actually been set and is available.
  • 2:36
    Or to say it differently, when you use this class,
  • 2:38
    you need to make sure any global or static variables it needs
  • 2:41
    are set up.
  • 2:42
    And the only way to know what the class needs
  • 2:45
    is to scan the file looking for global or static variable calls.
  • 2:49
    This makes FriendHarvester harder to understand and maintain
  • 2:52
    and much harder to test.
  • 2:55
    Let's get rid of all of that and do this right.
  • 3:00
    Since FriendHarvester needs the PDO object,
  • 3:03
    add a __construct() method with it as the first argument.
  • 3:11
    Set the value to a new private property
  • 3:13
    and update our code to use it.
  • 3:18
    The FriendHarvester now makes a lot of sense.
  • 3:21
    Whoever instantiates it must pass a PDO variable.
  • 3:25
    Inside this class, we don't care how this will happen.
  • 3:28
    We just know that it will and we can make use of it.
  • 3:31
    This very simple idea is called dependency injection.
  • 3:35
    And you just nailed it!
  • 3:37
    Dependency injection means that if a class needs an object
  • 3:40
    or some configuration, we force that information to be passed
  • 3:43
    into that class, instead of reaching outside of it
  • 3:46
    by using a global or static variable.
  • 3:49
    Back in app.php, we need to explicitly pass the PDO object
  • 3:53
    when instantiating the FriendHarvester.
  • 4:01
    Everything works exactly like before, except that we've
  • 4:04
    moved our logic into a service, which makes it testable, reusable,
  • 4:09
    and much more understandable for two reasons.
  • 4:12
    First, the class and method names serve
  • 4:14
    as the documentation for what our code does.
  • 4:18
    Second, because we're using dependency injection,
  • 4:21
    it's clear what our service might do,
  • 4:23
    because we can see what outside things it needs.

Services and Dependency Injection

Loading...

Our app is small now, but as it grows, the app.php file will get harder and harder to read. The best way to fix this is to separate each different chunk of functionality into different PHP classes and methods. Each of these classes is called a “service” and the whole idea is sometimes called Service-Oriented Architecture. In this tutorial you're going to create a new service, and learn about different ways to access external objects from a service. Then, we'll set up our app to use dependency injection to get the job done right.

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

Service-Oriented Architecture
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.