In Laravel, any downside to using App::make('') rather than constructor injection?

前端 未结 2 455
借酒劲吻你
借酒劲吻你 2021-01-18 14:21

Normally I would just inject dependancies via the constructor, but it gets to be very verbose when the parent class has dependancies and have to pass them through all the ch

2条回答
  •  囚心锁ツ
    2021-01-18 15:19

    There is a downside to your approach, doing what you propose will make your application less testable.

    What I mean is that if you try to write a unit test for the parent class you will no longer be testing that parent class in isolation. Your test now also depends on the results from the dependency declared inside the parent class.

    If you pass this dependency in through constructor injection (or any type of injection) you have control over the dependency and can mock/stub the output from it and better test your parent class in isolation.

提交回复
热议问题