Advertisement
chan15

DI Scenario 1

Jun 10th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. <?php
  2.  
  3. interface GameInterface
  4. {
  5.     public function checkLevel();
  6. }
  7.  
  8. class Game
  9. {
  10.     public $game;
  11.  
  12.     public function __construct(GameInterface $game)
  13.     {
  14.         $this->game = $game;
  15.     }
  16.  
  17.     public function play()
  18.     {
  19.         $className = get_class($this->game);
  20.  
  21.         if ($this->game->checkLevel() === true) {
  22.             return "{$className} can play";
  23.         }
  24.  
  25.         return "{$className} can't play";
  26.     }
  27. }
  28.  
  29. class Mario implements GameInterface
  30. {
  31.     public function checkLevel()
  32.     {
  33.         return true;
  34.     }
  35. }
  36.  
  37. class Race implements GameInterface
  38. {
  39.     public function checkLevel()
  40.     {
  41.         return false;
  42.     }
  43. }
  44.  
  45. echo (new Game(new Mario))->play() . PHP_EOL;
  46. echo (new Game(new Race))->play() . PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement