4. Pass objects in.
Instead of instantiating needed objects inside your
class, pass those needed objects in.
Do this:
● In your class constructor
● On a setter method
5. Without DI
// Foo is my dependency
function myFunction() {
$foo = new Foo;
return $foo->bar();
}
$bar = myFunction();
8. Benefits
➔ Single Responsibility
Because your class needn’t be
concerned with creating things to work.
➔ Flexibility
You class just “uses” things it’s passed,
so you can pass it other things without
changing the implementation.
➔ Clarity
You can see up front what the
dependencies of your class are..
➔ Testable
Easier to Unit Test your class if your
tests can control dependencies.