A dependency is another class that your class needs to function.
If you have a Model class that fetches data from a DB Object we can say that the Model class has a dependency on the DB Object.
Injecting Dependencies
A dependency is pushed into a class from the outside.
In the aforementioned example we would push a dependency into the Model class via a constructor parameter (or a setter) instead of directly instantiating new dependencies from inside the Model class.
public function __construct(Database $db)
Benefits of programming against interfaces in your code
- Decouples your classes construction from the construction of its dependencies.
- Test Driven Development
- Implementing an IoC Container such an Ninject (freeware)
- Adhering to the Dependency Inversion Principle (stated below)
Code should depend on abstractions (interfaces) rather than concrete implementations.
We can substitute different dependencies as long as they implement the required interface (as shown below)
No comments:
Post a Comment