S
S
symnoob2019-11-18 19:27:43
symfony
symnoob, 2019-11-18 19:27:43

How does it work in symfony via Interface?

Hello everyone, can anyone help me figure out the interface and DI container?
Take for example the same knp_pagination bundle. Inject the interface, pass parameters. And you're done. Such a complex mechanism as pagination works without problems. I don't understand this magic. I thought interfaces worked like instructions. There are methods that need to be filled in with code. And return something. And then somehow I passed the parameters and that's it ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Vasilkov, 2019-11-28
@gzhegow

I won't answer you how it works internally in symphony, but I'll try to add something about interfaces.
In general, an interface is a contract, similar to the one they give you at work when they accept you.
They are not filled with code, they are, as it were, an agreement between the parties - in our case, two classes - that what is "thrown through the interface" must be able to do what is written in the contract, otherwise the program will not compile and cannot be executed.
From a dependency manager's point of view, an interface is just a name. The same as just the string "app.moduleVasya". Further, this will be thrown through the constructor, which, according to the logic of PHP, will check what was created and slipped under this name - can what is written in the interface? Not? Mistake. In addition, PHP itself, even before the creation of objects, when you write implements The interface checks whether the class can do what it implements - there is, as it were, a double bind
The interface is just an agreement of the parties, on the other hand, the interface is a powerful guarantee of the ability to do something. That is, it doesn't matter what the object is called "grisha.module", "petya.supermodule" or "vasya.hypermodule" - they must be able to do one, two, three. Thus, we can nail the contract to the performers, and then quietly replace one with the other so that no one notices and nothing breaks.
You can simply create a certain class, accepting for yourself that "for now, I have enough of it, I'm not going to change it" - thus the module that understands which class depends on whom - simply executes the new Something() instruction and throws in the parameters (recursively ) that you specified. These can be things that you explicitly specify in the config, or they can be exactly the same modules as the one that is currently being built.
That is, the dependency manager creates an object, reading its constructor, and tries to throw in what can be created using the specified data types. What can not be created - she swears, they say - indicate this in the configuration or pass it manually at the time of creation. But since it is not customary in symphony to create objects and access the dependency manager directly, it works by itself, and if there is nothing to fill in some parameter, it swears.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question