Google+
Shineyrock web design & consultancy

Shineyrock

blog

  • dislike -2 08

    Examples of Dependency Injection in PHP With Symfony Components

    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.

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

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

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

    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.

    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.

    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.

    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.

    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.

    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.

    martijn broeders

    founder/ strategic creative at shineyrock web design & consultancy
    e-mail: .(JavaScript must be enabled to view this email address)
    phone: 434 210 0245

By - category

    By - date