W
W
WebDev2020-08-01 15:32:25
PHP
WebDev, 2020-08-01 15:32:25

Some php questions?

Good afternoon. Several questions have accumulated, tell me how to do it right.
1) Laravel has a container service. It is recommended to inject the desired class into the constructor or, if you are outside the container, then use make: That is, you should never create objects through new at all? Only through type-hint or make? With injection, everything is clear, but I don’t understand what flexibility make gives, because in the case of a class replacement, we still have to go and change the class in all places. There is still binding and you can inject an interface class and get an instance of its class, but it says right there:
$api = $this->app->make('HelpSpot\API');

There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection.

Should I create all my classes through interfaces and bind them?

2) Already asked, but did not receive an answer.
class Gibhub implements VCSInterface
{
    public function getRepositories(string $author) array
    {
        $response = Http::get('api.github.com/...')->json();

        if (!isset($response['key']) {
            throw new Exception('Not found');
        }

        return $response;
    }
}

I have code like this, but PhpStorm highlights "throw new Exception" with comment "Unhandled Exception". Why is he doing this? It is planned to call this method and wrap it in a try-catch.

3) I read this recommendation about Laravel
It's better not to use query builders inside controllers
.
Is it good practice to put all model work in separate classes and keep controllers thin?

Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question