Answer the question
In order to leave comments, you need to log in
Where can I find the simplest explanation of the Dependency Injection pattern?
Hello everyone, as usual, patterns are often difficult to understand, and I would like to clearly and clearly understand the principle of Dependency Injection using the simplest example. Maybe someone knows and here it will be described popularly with comments. I will be very grateful.
And also +
Servcie Locator;
Weakly understand this, but maybe they closely interact with each other.
Please examples in PHP
Answer the question
In order to leave comments, you need to log in
Martin Fowler writes cool about all the patterns. You can read about DI here . He actually has a great blog. And he's also the author of P of EAA . True, I highly do not recommend reading her Russian translation, you can only get confused, so read in the original.
If you want to deal with patterns, then the simplest (and at the same time efficient!) Book is Freeman & Freeman . It can also be read in Russian.
With regard to PHP, this is the best book on templates (and not only) that I have seen PHP. Objects, Patterns, and Programming Techniques by Mat Zandstra.
I recommend the following reading order: Freeman & Freeman, then Mat Zandstra, and for dessert Fowler's P of EAA.
It is important to distinguish the Dependency Injection pattern from the Dependency Injection Container.
The simplest example of dependency injection:
interface IEngine {}
class V8Engine implements IEngine {}
class Car {
public function __constructor(IEngine $engine) {
$this->engine = $engine;
}
}
$car = new Car(new V8Engine());
class V8Engine {}
class Car {
public function __constructor() {
$this->engine = new V8Engine();
}
}
$car = new Car();
// define some services
$container['session_storage'] = function ($c) {
return new SessionStorage('SESSION_ID');
};
$container['session'] = function ($c) {
return new Session($c['session_storage']);
};
In this video, Anthony Ferrara explains it as simply as possible:
www.youtube.com/watch?v=IKD2-MAkXyQ
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question