chan15

DI Scenario 2

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