Dependency Injection

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is dependency injection while the literal meaning is to inject dependencies so let's start by defining what a dependency is the dependency is just another object that your class needs to function so if you have a model class that fetches data from a database object we can say that your model class has a dependency of that database object so now that we know what a dependency is let's talk about what it means to inject dependencies injecting dependencies just means that the dependency is pushed into the class from the outside all that means is that you shouldn't instantiate dependencies using the new operator from inside of the class instead take it as a constructor parameter or via a setter that's really all there is to dependency injection you don't need a fancy container or a class or an object to do it sure they may make your life easier but you don't need them but why should we inject dependencies in the first place let's imagine for a minute that you're programming a house building robot you start with a pile of lumber and you program it to start building walls then when you get to a doorway what do you do do you program it to build a custom door out of raw materials each time or do you program it to take a ready-made door from the supplier and install that the most flexible way to do it would be to take the door from a supplier and that's exactly what dependency injection does it decouples your classes construction from the construction of its dependencies the reason that this is so important is the dependency inversion principle basically dependency inversion is the principal that code should depend upon abstractions by depending upon abstractions we're decoupling our implementations from each other in PHP that means your code should depend upon in their faces that way we can substitute different dependencies as long as they all satisfy the required interface by using dependency injection we decouple our code from the lower-level implementations making our code cleaner easier to modify and easier to reuse now that we've adopt the dependency injection we have another problem each of our classes require all of these dependencies so now to construct each and every class we not only need to figure out what dependencies they need we need to figure out how to instantiate the dependencies luckily for us there's a solution enter the dependency injection container at the root the container is nothing more than a map of dependencies that your class needs with the logic to create those dependencies if they haven't been created yet so every time you ask for a dependency the map will figure out which dependency to use and then the container will check to see if it created one of those dependencies already if it has it'll just use that one otherwise it'll create the dependency store it and then return it so instead of constructing all of your classes yourself you ask the container for a new instance it will then resolve the dependencies construct your object and return it to you the best part of it is that the container can resolve complex dependencies transparently and if you want to swap out a generic dependency you only need to update the container so right cleaner and more modular code use dependency injection you
Info
Channel: Anthony Ferrara
Views: 796,523
Rating: 4.9278164 out of 5
Keywords: Programming With Anthony, Programming, PHP, Dependency Injection, Object Oriented Programming
Id: IKD2-MAkXyQ
Channel Id: undefined
Length: 4min 47sec (287 seconds)
Published: Wed Jan 09 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.