Categories
Code PHP

What is a Dependency Injector

Sounds like a simple question right? I have had this question a number of times, and I had to question my self. What is the actually definition of this term? I know I use this design pattern, but how do I put it in words? Turns out it is actually pretty simple.

A great quote I found is:

“Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.”

http://www.picocontainer.org/injection.html

What does that mean? take for example is a user class needs to use the session class. Instead of creating a new instance of the session with in the user, it is created outside of the user class and then pass in via the constructor (or a method or directly into fields)

I found this site did a good job of explaining it with examples

http://fabien.potencier.org/what-is-dependency-injection.html

Leave a Reply