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.
$composer require symfony/dependency-injection
That should have created the composer.json file, which should look like this:
{ "require": { "symfony/dependency-injection": "^4.1", } }
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.
$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.
$composer require symfony/config
Let’s modify the composer.json file to look like the following one.
{ "require": { "symfony/dependency-injection": "^4.1", "symfony/config": "^4.1", "symfony/yaml": "^4.1" }, "autoload": { "psr-4": { "Services\": "src" }, "classmap": ["src"] } }
As we’ve added a new classmap entry, let’s go ahead and update the composer autoloader by running the following command.
$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.
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.
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.
register('demo.service', 'ServicesDemoService'); // fetch service from the service container $demoService = $containerBuilder->get('demo.service'); echo $demoService->helloWorld();To start with, we initialized the
ContainerBuilder
object with thenew ContainerBuilder()
constructor. Next, we used theregister
method of theContainerBuilder
object to inject our custom serviceServicesDemoService
into the container. Thedemo.service
acts as an alias to our service.Finally, we used the
get
method of theContainerBuilder
object to fetch our service from the container and used it to call thehelloWorld
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 theDemoService
service, created in the previous section, as a dependency. Thus, we'll see how theDemoService
service is automatically injected as a dependency when theDependentService
service is instantiated.Go ahead and create the src/DependentService.php file with the following contents to define the
DependentService
service.demo_service = $demoService; } public function helloWorld() { return $this->demo_service->helloWorld(); } }As you can see, the
ServicesDemoService
service is required in order to instantiate theDependentService
service.Next, go ahead and create the di_container.php file with the following contents.
register('demo.service', 'ServicesDemoService'); // add dependent service into the service container $containerBuilder->register('dependent.service', 'ServicesDependentService') ->addArgument(new Reference('demo.service')); // fetch service from the service container $dependentService = $containerBuilder->get('dependent.service'); echo $dependentService->helloWorld();We're using the same
register
method to inject our custom serviceServicesDependentService
into the container.In addition to that, we've also used the
addArgument
method to inform the container about the dependency of theDependentService
service. We've used theReference
class to inform the container that it needs to inject thedemo.service
service when thedependent.service
service is initialized. In that way, a dependency is automatically injected as needed!Finally, we've used the
get
method of theContainerBuilder
object to fetch thedependent.service
service from theContainerBuilder
object and used it to call thehelloWorld
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 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.
services: demo.service: class: ServicesDemoService dependent.service: class: ServicesDependentService arguments: ["@demo.service"]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.
load('services.yaml'); // fetch service from the service container $serviceOne = $containerBuilder->get('dependent.service'); 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.
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 new framework approach for our PHP community!
Share your thoughts and suggestions using the feed below. I'd love to discuss with you further!
Powered by WPeMatico