Guest User

Untitled

a guest
May 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. //Define the Vehicle interface
  4. interface Vehicle
  5. {
  6.     public function addLuggage($strContainer);
  7. }
  8.  
  9. //Define the Car class
  10. class Car implements Vehicle
  11. {
  12.     var $arrLuggage=array();
  13.     var $strCarType;
  14.    
  15.     function __construct($luggage = null)
  16.     {
  17.         if($luggage){
  18.             $this->addLuggage($luggage);
  19.         }
  20.     }
  21.    
  22.     function addLuggage($strContainer)
  23.     {
  24.         $this->arrLuggage[]=$strContainer;
  25.     }
  26. }
  27.  
  28. class Sports extends Car
  29. {
  30.     function __construct($luggage = null)
  31.     {
  32.         $this->strCarType="Sports";
  33.         parent::__construct($luggage);     
  34.     }
  35.    
  36.     function addLuggage($strContainer) {
  37.         if($strContainer->strLuggageType!="Suitcase") {
  38.             parent::addLuggage($strContainer);
  39.         } else {
  40.             //Throw exception if luggage type for Sports car is Suitcase
  41.             throw new Exception("The luggage doesn't fit");
  42.         }
  43.     }
  44. }
  45.  
  46. class StationWagon extends Car
  47. {
  48.     function __construct($luggage = null)
  49.     {
  50.         $this->strCarType="StationWagon";
  51.         parent::__construct($luggage);
  52.     }
  53. }
  54.  
  55. //Define the Container class
  56. class Container
  57. {
  58.     var $strLuggageType;
  59.  
  60.     function __construct()
  61.     {
  62.        
  63.     }
  64. }
  65.  
  66. class Suitcase extends Container
  67. {
  68.     function __construct()
  69.     {
  70.         $this->strLuggageType="Suitcase";
  71.     }
  72. }
  73.  
  74. class Bag extends Container
  75. {
  76.     function __construct()
  77.     {
  78.         $this->strLuggageType="Bag";
  79.     }
  80. }
  81.  
  82. $objNewCar=new Sports(new Bag());
  83.  
  84. $objNewCar=new Sports(new Suitcase());
  85.  
  86. print_r($objNewCar->arrLuggage);
  87.  
  88. ?>
Add Comment
Please, Sign In to add comment