F
F
Fedooot012021-08-19 20:35:25
Laravel
Fedooot01, 2021-08-19 20:35:25

Why is Dependency Injection implemented in Laravel via function parameters?

Hello.

It didn’t immediately dawn on me that in Laravel, if you write something like public function some_func(Request $request)$request in the method parameters, a class from a suitable service provider will be automatically created. According to the logic of past programming, on the contrary, I had to pass the $request variable of the Request class to this some_func when called.

Why is it done this way? Because it's confusing. Why not get the necessary objects by the type of facades or some other function that would return the requested values ​​from anywhere.
What is the advantage of passing in the parameters of methods?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
inFureal, 2021-08-19
@Fedooot01

Nothing prevents you from doing it through the helper app. Internally, make is called in the ServiceContainer
$service = app(YourCoolService::class);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question