mf

Examples of Dependency Injection in PHP With Symfony Components

In this article, we’ll look at some examples of using the Symfony DependencyInjection component. You’ll learn the basics of dependency injection, which allows cleaner and more modular code, and you’ll see how to use it in your PHP application 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, the container is responsible for creating services as required. More importantly, the 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.

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

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 that comes to the rescue as it helps you to convert YAML strings to PHP-compatible data types and vice versa.

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.

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

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

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.

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 the 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.

Powered by WPeMatico

Leave a Comment

Scroll to Top