Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ancutaciubotaru
Courses Plus Student 16,310 PointsArticle about PHP dependency injection?
Does anyone know a good article about PHP dependency injection?
I heard that it helps with unit testing, but I'm not sure I fully understand how it is supposed to do that.
I guess, my main concern is how to keep the variables created organized.
1 Answer
Andrew Shook
31,709 PointsAncutaciubotaru, dependency injection has more to do with encapsulation than it has to do with the number of parameters in an object's constructor. Encapsulation states that an object should only know about and be concerned with the one thing it does. Sure, one object may have to interact with another object, but neither object needs to know how to create the other or how the other object works internally. A real world example would me a carpenter(object A). A carpenter doesn't need to know how a hammer(object B) is made or the physics behind how it works, a carpenter simply need to know how to swing the hammer(this would be a method of the carpenter class).
In the article you linked above, the author was trying to point out that the Product class doesn't need to create the StockItem class( read as KNOW HOW to make the stock item class). Instead, the Product class only needs to be given the StockItem class so that the Product class can use it to do whatever it need to with it. On the same note, the Product class may need to access the DB to get information, but the Product class doesn't need to know how the DB class is made or how it works internally. The Product class just needs to know the interface of the classes it uses( i.e. the methods it can call on that class). So how does all of this help with programming? Well If all objectA knows about objectB is its interface, then I can change how objectB works internally without needing to change anything in objectA as long as objectB has all the methods object A uses. A real world example would be the OS on a computer. You, as the user, don't need to know how the OS works, because all you use is the computer's interface(keyboard, mouse, monitor). So if the internal programming changes, it has no effect on how you interact with the computer.
Now, how does all of this wonderful OOP principles help with unit testing. In the first example of the article you linked, you wouldn't be able to do unit testing because the Product class would have to create a StockItem class for the test to run. Since the unit test is only running that one file, the test would always fail because the StockItem Class code isn't available to the Product class code. However, if your use Dependency Injection, you can create what is called a "mock", or fake StockItem object and have the unit testing script pass the mock into the Product class for testing.
ancutaciubotaru
Courses Plus Student 16,310 PointsThank you Andrew for your answer!
ancutaciubotaru
Courses Plus Student 16,310 Pointsancutaciubotaru
Courses Plus Student 16,310 PointsSo far I found this article: http://coderoncode.com/2014/01/06/dependency-injection-php.html which describes constructor and setter injections.
So this means that the Dependency Injection pattern is about keeping the code clean by reducing the number of mandatory parameters?
Is this all? Am I missing something?