Wednesday, August 8, 2018

Examples of Dependency Injection in PHP With Symfony Components

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 the dependency management in your PHP applications. The heart of the DependencyInjection component is a container which holds all 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 the later stage, the container is responsible for creating services as required. More importantly, container is also responsible for creating and injecting dependencies of 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 that should look like:

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 file 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 from as needed.

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

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.

To start with, we initialized the ContainerBuilder object with the new ContainerBuilder() constructor. Next, we've 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.

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.

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 initialized. 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 last section, we'll explore how you could dynamically load services from the YAML file. Basically, we'll update the example from 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.

As you can see, it's pretty straightforward to define services using the YAML syntax. To define 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.

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.

Conclusion

The Symfony DependencyInjection component took the 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 benefits of this new framework approach for our PHP community!

Share your thoughts and suggestions using the feed below. I'd love to discuss with you further!


No comments:

Post a Comment