D
D
dllweb2015-02-28 17:27:11
symfony
dllweb, 2015-02-28 17:27:11

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

2 answer(s)
I
Ivan Velichko, 2015-02-28
@dllweb

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());

The simplest example of ignoring explicit injection (it is difficult to write unit tests for such code, it is more difficult to understand and edit it):
class V8Engine {}

class Car {
  public function __constructor() {
    $this->engine = new V8Engine();
  }
}

$car = new Car();

A great (and lightweight) example of a DIC is pimple :
// define some services
$container['session_storage'] = function ($c) {
    return new SessionStorage('SESSION_ID');
};

$container['session'] = function ($c) {
    return new Session($c['session_storage']);
};

I advise you to read and understand its sources to make sure that there is no magic in DIC (at least for PHP). The first version was only ~100 lines. It should also be noted that the Session class uses the Dependency Injection pattern, explicitly defining its dependency on SessionStorage. And the container makes only the correct bunch.
And yes, the container itself can be used as a service locator if, for example, it has global access. But this is a very bad practice, because if something accesses the service locator, then formally it begins to depend on all system components at once.

B
BoneFletcher, 2015-03-05
@BoneFletcher

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 question

Ask a Question

731 491 924 answers to any question