Dependancy Injection and Unit Testing
Dependence injection isn't a new concept but is not yet widespread throughout the PHP community. One of the large advantages to using dependancy injection for objects is when coupled with Unit Testing. By injecting the dependancy as apposed to the object managing its dependancies itself allows your Unit Tests to test the object, and not any of the dependancies. See Mock Objects.
Options for Dependancy Injection
Generally you have 2 options for dependancy injection. Passing via the constructor or using a setter/getter. A quick example of using dependancy injection could be a computer. The computer relies on its CPU to work, we want to test the computer but not the CPU.
Example of dependancy injection
A quick example of a class using dependancy injection can be seen below. This Computer class allows us to replace the CPU with an alternative one. What this also allows, however, is for us to create a Mock Object in replace of the CPU which stops the CPU class from affecting the outcome of tests targeted at our computer.