Answer the question
In order to leave comments, you need to log in
How to comply with the principle of the last letter SOLID?
I've been working with the "Dependency Injection" template for a couple of days, and I have a few questions:
1. Let's say I have a dependency for a router in the form of a controller. It is not known in advance which controller it is, it will be known during the execution of the program. The controller instance is created in the router. How can I provide a router with a controller? After all, I can’t write $controllerObj = new $controller();
, because it creates a dependency on the class of some controller.
My solution: pass the dependency to the router in the form of an abstract controller, and its descendants will automatically suck in runtime.
2. What if one of the child controllers needs some kind of dependency? I saw a solution where the DI container was passed down through the controller constructors down and down the chain to where it was needed. Is this the right solution? But then it creates a dependency on the container itself. How then to be?
My decision: no.
In general, I want you to be guided on the true path, to be told whether I am thinking in the right direction and whether the solution to my first problem is correct.
Answer the question
In order to leave comments, you need to log in
First, I advise you not to do bullshit, taking all sorts of stupid courses and making your own bikes. Believe me, these courses will not teach you anything good, I went through all this myself - exactly zero sense. You still won’t get anything good, as the best developers hone their frameworks for years, while borrowing chips from each other (we are talking about popular frameworks).
Secondly, if you still have an awl in your ass, you want to fence some kind of garbage, then smart people have come up with some standards. The conditional standard in php are components from the highest quality symfony framework, on which, by the way, laravel is also based. Here is a router component for example. Popular frameworks are made up of such small independent components.
Thirdly, for the future .. Before you do something, look at how others have done it. I gave you the route component, if you want to cut your own (although this is absolutely pointless), then you are welcome, but you won’t do 100% better, so just look at how they did it at least.
Well, now I will answer your question. Controllers are bound to the router from the outside, so there will be no bindings inside. And in general, it’s not worth talking about SOLID here, since this is the stage of loading the application. No one can be sure that such a controller exists at all and that such a method will be present in it, so in this case there will simply be an exception.
By the way, you can look at these courses, the most sensible of what is
see how here https://github.com/dimaxz/frameworkless/blob/maste...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question