1. Code
  2. Coding Fundamentals
  3. Design Patterns

Examples of Dependency Injection in PHP With Symfony Components

Scroll to top

In this article, we'll look at a few examples of using the Symfony DependencyInjection component. You'll learn the basics of dependency injection, which allows clean and modular code, and you'll see how to use it in your PHP applications with the Symfony component.

What Is the Symfony DependencyInjection Component?

The Symfony DependencyInjection component provides a standard way to instantiate objects and handle dependency management in your PHP applications. The heart of the DependencyInjection component is a container, which holds all the available services in the application.

During the bootstrapping phase of your application, you're supposed to register all services in your application into the container. At a later stage, a container is responsible for creating services as needed. More importantly, a container is also responsible for creating and injecting dependencies of the services.

The benefit of this approach is that you don't have to hard code the process of instantiating objects, since dependencies will be detected and injected automatically. This creates a loose coupling between parts of your application.

In this article, we'll explore how you can unleash the power of the DependencyInjection component. As usual, we'll start with installation and configuration instructions, and we'll implement a few real-world examples to demonstrate the key concepts.

Installation and Configuration

In this section, we'll go ahead and install the DependencyInjection component. I assume that you've already installed Composer in your system as we'll need it to install the DependencyInjection component available at Packagist.

So go ahead and install the DependencyInjection component using the following command.

1
$composer require symfony/dependency-injection

That should have created the composer.json file, which should look like this:

1
{
2
    "require": {
3
        "symfony/dependency-injection": "^5.4"
4
    }
5
}

We'll also install a few other components that will be useful in our examples.

If you want to load services from a YAML file instead of defining it in the PHP code, it's the Yaml component which comes to the rescue as it helps you to convert YAML strings to PHP-compatible data types and vice versa.

1
$composer require symfony/yaml

Finally, we'll install the Config component which provides several utility classes to initialize and deal with configuration values that are defined in different types of files like YAML, INI, and XML. In our case, we'll use it to load services from the YAML file.

1
$composer require symfony/config

Let's modify the composer.json file to look like the following one.

1
{
2
    "require": {
3
        "symfony/dependency-injection": "^5.4",
4
        "symfony/yaml": "^5.4",
5
        "symfony/config": "^5.4"
6
    },
7
    "autoload": {
8
         "psr-4": {
9
             "Services\\": "src"
10
         },
11
         "classmap": ["src"]
12
    }
13
}

As we've added a new classmap entry, let's go ahead and update the composer autoloader by running the following command.

1
$composer dump -o

Now, you can use the Services namespace to autoload classes under the src directory.

So that's the installation part, but how are you supposed to use it? In fact, it's just a matter of including the autoload.php file created by Composer in your application, as shown in the following snippet.

1
<?php
2
require_once './vendor/autoload.php';
3
 
4
// application code

5
?>

How to Work With a Container

In this section, we'll go through an example to demonstrate how you could inject services into a container. A container should act as a central repository which holds all services in your application. Later on, we could use a container to fetch services as needed.

To start with, let's go ahead and define a pretty basic service at src/DemoService.php with the following contents.

1
<?php
2
// src/DemoService.php

3
namespace Services;
4
 
5
class DemoService
6
{
7
  public function helloWorld()
8
  {
9
    return "Hello World!\n";
10
  }
11
}

This is a very simple service, which just implements the helloWorld method for the moment.

Next, go ahead and create the basic_container.php file with the following contents in the root of your application.

1
<?php
2
// basic_container.php

3
require_once './vendor/autoload.php';
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
 
6
// init service container

7
$containerBuilder = new ContainerBuilder();
8
 
9
// add service into the service container

10
$containerBuilder->register('demo.service', '\Services\DemoService');
11
 
12
// fetch service from the service container

13
$demoService = $containerBuilder->get('demo.service');
14
echo $demoService->helloWorld();

To start with, we've instantiated the ContainerBuilder object with the new ContainerBuilder() constructor. Next, we used the register method of the ContainerBuilder object to inject our custom service \Services\DemoService into the container. The demo.service acts as an alias to our service.

Finally, we've used the get method of the ContainerBuilder object to fetch our service from the container and used it to call the helloWorld method.

So that was a basic demonstration of how to work with a container. In the next section, we'll extend this example to explore how the class dependencies are resolved using a container.

A Real-World Example

In this section, we'll create an example which demonstrates how class dependencies are resolved using the DependencyInjection component.

To demonstrate it, we'll create a new service DependentService which requires the DemoService service, created in the previous section, as a dependency. Thus, we'll see how the DemoService service is automatically injected as a dependency when the DependentService service is instantiated.

Go ahead and create the src/DependentService.php file with the following contents to define the DependentService service.

1
<?php
2
// src/DependentService.php

3
namespace Services;
4
 
5
class DependentService
6
{
7
  private $demo_service;
8
 
9
  public function __construct(\Services\DemoService $demoService)
10
  {
11
    $this->demo_service = $demoService;
12
  }
13
 
14
  public function helloWorld()
15
  {
16
    return $this->demo_service->helloWorld();
17
  }
18
}

As you can see, the \Services\DemoService service is required in order to instantiate the DependentService service.

Next, go ahead and create the di_container.php file with the following contents.

1
<?php
2
// di_container.php

3
require_once './vendor/autoload.php';
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\DependencyInjection\Reference;
6
 
7
// init service container

8
$containerBuilder = new ContainerBuilder();
9
 
10
// add demo service into the service container

11
$containerBuilder->register('demo.service', '\Services\DemoService');
12
 
13
// add dependent service into the service container

14
$containerBuilder->register('dependent.service', '\Services\DependentService')
15
                 ->addArgument(new Reference('demo.service'));
16
 
17
// fetch service from the service container

18
$dependentService = $containerBuilder->get('dependent.service');
19
echo $dependentService->helloWorld();

We're using the same register method to inject our custom service \Services\DependentService into the container.

In addition to that, we've also used the addArgument method to inform the container about the dependency of the DependentService service. We've used the Reference class to inform the container that it needs to inject the demo.service service when the dependent.service service is instantiated. In that way, a dependency is automatically injected as needed!

Finally, we've used the get method of the ContainerBuilder object to fetch the dependent.service service from the ContainerBuilder object and used it to call the helloWorld method.

In this way, the DependencyInjection component provides a standard way to instantiate objects and inject dependencies in your application.

How to Dynamically Load Services Using the YAML File

In this section, we'll explore how you could dynamically load services from the YAML file. Basically, we'll update the example discussed in the previous section.

In addition to the DependencyInjection component, we'll also need two more Symfony components to implement the YAML example—Config and Yaml. Recall that we've already installed these two components in the Installation and Configuration section along with the DependencyInjection component itself. So we're good to go!

Go ahead and create the services.yaml file with the following contents in the root of your application.

1
services:
2
    demo.service:
3
        class:     \Services\DemoService
4
    dependent.service:
5
        class:     \Services\DependentService
6
        arguments: ["@demo.service"]

As you can see, it's pretty straightforward to define services using the YAML syntax. To define the dependencies of your service, you'll need to use the arguments key.

Next, go ahead and create the di_yaml_container.php file with the following contents.

1
<?php
2
// di_yaml_container.php

3
require_once './vendor/autoload.php';
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
7
 
8
// init service container

9
$containerBuilder = new ContainerBuilder();
10
 
11
// init yaml file loader

12
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__));
13
 
14
// load services from the yaml file

15
$loader->load('services.yaml');
16
 
17
// fetch service from the service container

18
$serviceOne = $containerBuilder->get('dependent.service');
19
echo $serviceOne->helloWorld();

Everything is pretty much the same except that we're loading services from the services.yaml file instead of defining it in the PHP code itself. This allows the application dependencies to be defined dynamically.

How to Inject a Lazy Service

In some cases, you want to inject a lazy service. Sometimes, you have a service which is very heavy to instantiate. So you only want such a service to be injected when it's really needed, and not before that. The answer to this question is a lazy service.

But how does this work exactly? In fact, when you configure a lazy service, instead of injecting the actual service, a proxy of the service is injected. On the surface, a proxy service acts like the actual service, but as soon as you start interacting with the proxy service, the actual service is instantiated.

To use lazy services, we need to install the symfony/proxy-manager-bridge package. Let's do that in the first place.

1
$composer require symfony/proxy-manager-bridge

Next, we'll revise the di_container.php example to understand how to use the proxy manager to create and inject a proxy service.

Go ahead and replace the code of the di_container.php file with the following contents.

1
<?php
2
require_once './vendor/autoload.php';
3
4
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
5
use ProxyManager\Proxy\LazyLoadingInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
// code for creating proxy starts

9
$factory     = new LazyLoadingValueHolderFactory();
10
$initializer = function (& $wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, & $initializer) {
11
    global $containerBuilder;
12
    $initializer   = null; // disable initialization

13
    $wrappedObject = $containerBuilder->get('demo.service'); // fill your object with values here

14
15
    return true; // confirm that initialization occurred correctly

16
};
17
$proxy = $factory->createProxy('\Services\DemoService', $initializer);
18
// code for creating proxy ends

19
20
global $containerBuilder;
21
$containerBuilder = new ContainerBuilder();
22
$containerBuilder->register('demo.service', '\Services\DemoService');
23
$containerBuilder->register('dependent.service', '\Services\DependentService')->addArgument($proxy);
24
$dependentService = $containerBuilder->get('dependent.service');
25
26
echo $dependentService->helloWorld();

Firstly, we've created an instance of the LazyLoadingValueHolderFactory class. Next, we've used the createProxy method of this class to define how the instance of the \Services\DemoService class will be created. The second argument of the createProxy method is an anonymous function, which will be called when the actual instance of the class needs to be instantiated instead of a proxy object. And thus, the anonymous function handles the logic of creating an actual instance.

Apart from that, it's pretty much the same. Once the proxy instance is created, we'll pass it in the addArgument method, instead of creating an instance with the Reference class.

The benefit of this approach is that whenever we create an instance of the \Services\DependentService class, it won't create the actual object of the \Services\DemoService class—instead, it'll create a proxy object. The actual object of the \Services\DemoService class will be created only when you call any method of it.

To confirm it, you can just dump the $dependentService object, and you should see something like this.

1
Services\DependentService Object
2
(
3
    [demo_service:Services\DependentService:private] => ProxyManagerGeneratedProxy\__PM__\Services\DemoService\Generatedccdf8241e892da36c12500ed72d82829 Object
4
        (
5
            [valueHoldera1fd8:ProxyManagerGeneratedProxy\__PM__\Services\DemoService\Generatedccdf8241e892da36c12500ed72d82829:private] => 
6
            [initializere3996:ProxyManagerGeneratedProxy\__PM__\Services\DemoService\Generatedccdf8241e892da36c12500ed72d82829:private] => Closure Object
7
                (
8
                    [parameter] => Array
9
                        (
10
                            [&$wrappedObject] => <required>
11
                            [$proxy] => <required>
12
                            [$method] => <required>
13
                            [$parameters] => <required>
14
                            [&$initializer] => <required>
15
                        )
16
17
                )
18
19
        )
20
21
)

As you can see, it has created the proxy object instead of the actual \Services\DemoService object.

If you hadn't used the proxy method, the output would have been like this.

1
Services\DependentService Object
2
(
3
    [demo_service:Services\DependentService:private] => Services\DemoService Object
4
        (
5
        )
6
7
)

As you can see, it has created an instance of the \Services\DemoService class. So that's how lazy loading of services works!

Conclusion

The Symfony DependencyInjection component took center stage in this tutorial. We saw how to install and configure DependencyInjection, as well as some real-world examples of how it can be used.

I'm really fascinated and excited about the decoupled components of the Symfony framework that you can just pick and choose for your application. Plug them into your code, and they just work! All in all, I can only see the benefits of this framework approach for our PHP community.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.