title: constructor injection vs method injection
body: <p>Laravel encourages dependency injection. Since I am using laravel on my project, I figured I'll try using this approach.</p>
  
  <p>I am taking advantage of Laravel's service container by <a href="https://laravel.com/docs/5.5/controllers#dependency-injection-and-controllers" rel="nofollow noreferrer">type hinting my dependencies and letting it resolve them</a>. I have four controllers. All of them extend a base class called GlobalController. I also have two models. All of them extend a base class called GlobalModel.</p>
  
  <p>My first attempt is using method injection. GlobalController looks like this:</p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Models\GlobalModel;
  
  class GlobalController extends Controller
  {
  
      public function __construct()
      {
          $this-&gt;middleware(['authenticate', 'token']);
      }
  
      // functions that handle normal http requests and ajax requests
  
      }
  </code></pre>
  
  <p>One of the controllers that extends from GlobalController is called UserController. Some of its functions are:</p>
  
  <ul>
  <li>index - show all data</li>
  <li>edit - show edit form</li>
  <li>update - updates to database</li>
  </ul>
  
  <p>Edit and update use <a href="https://laravel.com/docs/5.5/routing#route-model-binding" rel="nofollow noreferrer">route-model-binding</a>.</p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Models\User;
  
  class UserController extends GlobalController
  {
  
      public function index(User $user)
      {
          $users = $user-&gt;all();
          return view('pages/view_users')-&gt;with('users', $users);
      }
  
      public function edit(User $user)
      {
          return view('pages/edit_user')-&gt;with('user', $user);
      }
  
      public function update(Request $request, User $user)
      {
          $data = $request-&gt;all();
          if ($user-&gt;validate($data))
          {
              $user-&gt;update($data);
              return $this-&gt;successResponse($request, 'users', 'Successfully edited user');
          }
          return $this-&gt;failedResponse($request, $user);
      }
  
      // other functions
  
      }
  </code></pre>
  
  <p>While this is working fine, Request and User get injected many times. If I have to change a Request implementation (for example), I'll have to manually change many functions to type hint that particular Request object. Not good at all. Since they are usually called in most functions, I tried doing constructor injection.</p>
  
  <p>Here is GlobalController using constructor injection:</p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Models\GlobalModel;
  
  class GlobalController extends Controller
  {
      protected $request;
      protected $model; // use polymorphism
  
      public function __construct(Request $request, GlobalModel $model)
      {
          $this-&gt;request = $request;
          $this-&gt;model = $model;
          $this-&gt;middleware(['authenticate', 'token']);
      }
  
      // functions that handle normal http requests and ajax requests
  
  }
  </code></pre>
  
  <p>And here is UserController using constructor injection containing the same functions:</p>
  
  <pre><code>namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;;
  use App\Models\User;
  
  class UserController extends GlobalController
  {
  
      public function __construct(Request $request, User $user) // use polymorphism
      {
          parent::__construct($request, $user);
      }
  
      public function index()
      {
          $users = $this-&gt;model-&gt;all();
          return view('pages/view_users')-&gt;with('users', $users);
      }
  
      public function edit(int $id)
      {
          $this-&gt;model = $this-&gt;model-&gt;find($id);
          return view('pages/edit_user')-&gt;with('user', $this-&gt;model);
      }
  
      public function update(int $id)
      {
          $this-&gt;model = $this-&gt;model-&gt;find($id);
          $data = $this-&gt;request-&gt;all();
          if ($this-&gt;model-&gt;validate($data))
          {
              $this-&gt;model-&gt;update($data);
              return $this-&gt;successResponse('users', 'Successfully edited user');
          }
          return $this-&gt;failedResponse();
      }
  
      // other functions
  
  }
  </code></pre>
  
  <p>Now, I can't put my finger on it, but I think this implementation seems not right. It became less readable. The usage of $model and $this have made the code more disgusting.</p>
  
  <p>I am so confused. I understand the benefits I can get from dependency injection, but I am sure my implementations of method injection and constructor injection are extremely wrong. What implementation should I choose? Or should I choose any from these two at all?</p>