Advertisement
Guest User

Dependency Injection in Laravel

a guest
Nov 27th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. <?php
  2.  
  3. // opción 1
  4. class MyController extends Controller {
  5.     protected $dep;
  6.     public function __construct(Dependency $dep) {
  7.         parent::__construct();
  8.         $this->dep = $dep;
  9.     }
  10.  
  11.     public function doStuff() {
  12.         $this->dep->blah();
  13.     }
  14. }
  15.  
  16. // opción 2
  17. class MyController extends Controller {
  18.     public function doStuff() {
  19.         $dep = App::make('Dependency');
  20.         $dep->blah();
  21.     }
  22. }
  23.  
  24. // test
  25. class MyControllerTest extends TestCase {
  26.     public function testBlah() {
  27.         $dep = Mockery::mock('Dependency');
  28.         $this->app->instance('Dependency', $dep);
  29.  
  30.         // ...
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement