In Laravel, is there any drawback to using App :: make ('') rather than installing a constructor?

Usually I just add the dependencies through the constructor, but it becomes very verbose when the parent class has dependencies and needs to pass them through all the children.

An alternative is to use $this->dependancy = App::make('Dependancy')only in the parent class. Then both the parent and child constructors can be empty. Is there a flaw to do it this way?

+4
source share
2 answers

There is a flaw in your approach, since doing what you offer will make your application less susceptible to testing.

, unit test , . , .

( ), / .

+3

, App::make('...') app('...'), , . , - :

class SomeController extends BaseController {

    protected $otherClass = null;

    public function __construct(SomeOtherClass $otherClass)
    {
        $this->otherClass = $otherClass;
    }
}

, SomeController, Laravel SomeOtherClass SomeController, SomeOtherClass , Laravel , . , Dependency Injection App::make(...)/app(...), , Interface concrete class. , Interface implementation ( ).

, , , class , . , - runtime constructor ( ), , , , , , .

: , Laravel, dependency , framework , type Laravel App::make(...). , , App::make(...), . , App::make() , . , App::make() - , .

, , Laravel App::make(...), , , , ' App::make(). , , , Laravel - , . , testing tho.

+2

All Articles