This content originally appeared on Envato Tuts+ Tutorials and was authored by Sajal Soni
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 cleaner 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.
$composer require symfony/dependency-injection
That should have created the composer.json file, which should look like this:
{ "require": { "symfony/dependency-injection": "^5.4" } }
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.
$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": "^5.4", "symfony/yaml": "^5.4", "symfony/config": "^5.4" }, "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.
<?php require_once './vendor/autoload.php'; // application code ?>
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.
<?php // src/DemoService.php namespace Services; class DemoService { public function helloWorld() { return "Hello World!\n"; } }
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.
<?php // basic_container.php require_once './vendor/autoload.php'; use Symfony\Component\DependencyInjection\ContainerBuilder; // init service container $containerBuilder = new ContainerBuilder(); // add service into the service container $containerBuilder->register('demo.service', '\Services\DemoService'); // fetch service from the service container $demoService = $containerBuilder->get('demo.service'); 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.
<?php // src/DependentService.php namespace Services; class DependentService { private $demo_service; public function __construct(\Services\DemoService $demoService) { $this->demo_service = $demoService; } public function helloWorld() { return $this->demo_service->helloWorld(); } }
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.
<?php // di_container.php require_once './vendor/autoload.php'; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; // init service container $containerBuilder = new ContainerBuilder(); // add demo service into the service container $containerBuilder->register('demo.service', '\Services\DemoService'); // add dependent service into the service container $containerBuilder->register('dependent.service', '\Services\DependentService') ->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 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.
services: demo.service: class: \Services\DemoService dependent.service: class: \Services\DependentService 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.
<?php // di_yaml_container.php require_once './vendor/autoload.php'; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; // init service container $containerBuilder = new ContainerBuilder(); // init yaml file loader $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__)); // load services from the yaml file $loader->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.
How to Inject a Lazy Service
In some cases, you want to inject a lazy service. Sometimes, you've a service which is very heavy to instantiate. So if you want to inject such service, you would wish that it is only 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.
$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.
<?php require_once './vendor/autoload.php'; use ProxyManager\Factory\LazyLoadingValueHolderFactory; use ProxyManager\Proxy\LazyLoadingInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; // code for creating proxy starts $factory = new LazyLoadingValueHolderFactory(); $initializer = function (& $wrappedObject, LazyLoadingInterface $proxy, $method, array $parameters, & $initializer) { global $containerBuilder; $initializer = null; // disable initialization $wrappedObject = $containerBuilder->get('demo.service'); // fill your object with values here return true; // confirm that initialization occurred correctly }; $proxy = $factory->createProxy('\Services\DemoService', $initializer); // code for creating proxy ends global $containerBuilder; $containerBuilder = new ContainerBuilder(); $containerBuilder->register('demo.service', '\Services\DemoService'); $containerBuilder->register('dependent.service', '\Services\DependentService')->addArgument($proxy); $dependentService = $containerBuilder->get('dependent.service'); 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.
Services\DependentService Object ( [demo_service:Services\DependentService:private] => ProxyManagerGeneratedProxy\__PM__\Services\DemoService\Generatedccdf8241e892da36c12500ed72d82829 Object ( [valueHoldera1fd8:ProxyManagerGeneratedProxy\__PM__\Services\DemoService\Generatedccdf8241e892da36c12500ed72d82829:private] => [initializere3996:ProxyManagerGeneratedProxy\__PM__\Services\DemoService\Generatedccdf8241e892da36c12500ed72d82829:private] => Closure Object ( [parameter] => Array ( [&$wrappedObject] => <required> [$proxy] => <required> [$method] => <required> [$parameters] => <required> [&$initializer] => <required> ) ) ) )
As you can see, it has created the proxy object instead of the actual \Services\DemoService
object.
Had you wouldn't have used the proxy method, the output would have been like this.
Services\DependentService Object ( [demo_service:Services\DependentService:private] => Services\DemoService Object ( ) )
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!
This content originally appeared on Envato Tuts+ Tutorials and was authored by Sajal Soni
Sajal Soni | Sciencx (2018-06-08T17:29:34+00:00) Examples of Dependency Injection in PHP With Symfony Components. Retrieved from https://www.scien.cx/2018/06/08/examples-of-dependency-injection-in-php-with-symfony-components/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.