Dependency Injection in PHP

What is Dependency Injection?

A Dependency is an object that can be used. An Injection is the passing of a dependency (object) to a dependent object that would use it. Eventually, Dependency Injection means giving the object all the dependencies (needed objects).

For example, a User object uses a Mailer object to send welcome email. In this example the Mailer object is a dependency for the User object. We can ascribe this dependency to classes as well. Now, we should pass Mailer instance to User object instead of creating new instance of Mailer in the User body.
Continue reading Dependency Injection in PHP