Answer the question
In order to leave comments, you need to log in
What is the difference between these two code snippets?
Yes, I don’t quite understand the essence of DI, even though I read a lot, but I only understood that an object of another class should be passed to the class constructor, and then the object of the passed class should be passed to the property (well, like here):
class Table {
public $cup;
public function __construct(Cup $cup) {
$this->cup = $cup;
}
}
public function update(RequestUpdatePost $request, $id) {}
public function update($id) {
$request = new RequestUpdatePost;
Answer the question
In order to leave comments, you need to log in
The RequestUpdatePost class is inherited from the base Request, it usually describes the rules for request validation, if the request is not valid, errors will fly and such a request will not pass into your method.
An example of how it might look:
DI solves the problem of hidden dependencies. Explicit is always better than implicit. And although the second option is not critical, but as soon as the RequestUpdatePost class requires more dependencies, you will have to get them from somewhere.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question