What is PHP D.I when and how to use it

Hi, can some one explain me about the PHP D.I, I am knew with this i am confuse what is the purpose of this,is this applicable if you use framework. Or also this can be use with our own code scratch without using any frameworks. ?

Thank you in advance.

Are you specifically asking about the DI library, PHP-DI?

Scott

I see I thought it’s all library, so what about in pure php without using the library ?
or what is Dependency Injection is all about ?

Thank you in advance.

Dependency injection is a fancy word describing the OOP design pattern, where objects are created in the client code and injected into other objects also needed within the client code. So, if you mean to ask, can you use DI without a library? The answer is, yes.

A more elaborate explanation is this one by Fabien Potencier, the father of the Symfony framework. He also goes from explaining the DI pattern to how Symfony uses DI for its DI container, which is called the Symfony service container.

DI is a design pattern. PHP DI is a library, which can build a DI container. In the second part of Fabien’s article, he even writes:

Most of the time, you don’t need a Dependency Injection Container to benefit from Dependency Injection.

But, if you read on, he does explain why it comes in handy.

So, you could say a DI container (the DI libraries like PHP DI) is the next logical step from the DI pattern, because it helps the client coder manage the dependencies found within the application.

Please note: the article from Fabien is a bit old and things have changed some within Symfony, since he wrote it. So, only read it and understand it from a conceptual standpoint.

Scott

1 Like

It means that instead of having classes create the objects they depend on:

class Car
{
    private $engine;

    public function __construct()
    {
        $this->engine = new Engine();
    }
}

you ‘inject’ those objects via the constructor or a method:

class Car
{
    private $engine;

    public function __construct(Engine $engine)
    {
        $this->engine = $engine;
    }
}

There are several benefits of doing this:

  • It is easy for a developer to see what the dependencies of a class are, they are not hidden within the class
  • It is easier to test the class - in the first example above, it’s impossible to test Car without also testing Engine. With DI, you can inject mock object (fake objects that pretend to be the dependencies for the purposes of testing)
  • It is easy to swap out dependencies. Let’s say we want to create a car with a more powerful engine. We can create a new Engine class and pass that into Car, and we don’t have to modify the Car class at all.
2 Likes

Although there is a good chance that if you swapped out you Engine class with another there would be some rouge method in there that doesn’t want to play nice so your better off writing an interface for it now.

Yeah, its coding to an interface, not an implementation. But it doesnt have to be a PHP interface, an abstract class will fit as well, and maybe even a concrete superclass.

Can I ask when we use D.I is this can be done always in constructor ?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.