Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Define the Vehicle interface
- interface Vehicle
- {
- public function addLuggage($strContainer);
- }
- //Define the Car class
- class Car implements Vehicle
- {
- var $arrLuggage=array();
- var $strCarType;
- function __construct($luggage = null)
- {
- if($luggage){
- $this->addLuggage($luggage);
- }
- }
- function addLuggage($strContainer)
- {
- $this->arrLuggage[]=$strContainer;
- }
- }
- class Sports extends Car
- {
- function __construct($luggage = null)
- {
- $this->strCarType="Sports";
- parent::__construct($luggage);
- }
- function addLuggage($strContainer) {
- if($strContainer->strLuggageType!="Suitcase") {
- parent::addLuggage($strContainer);
- } else {
- //Throw exception if luggage type for Sports car is Suitcase
- throw new Exception("The luggage doesn't fit");
- }
- }
- }
- class StationWagon extends Car
- {
- function __construct($luggage = null)
- {
- $this->strCarType="StationWagon";
- parent::__construct($luggage);
- }
- }
- //Define the Container class
- class Container
- {
- var $strLuggageType;
- function __construct()
- {
- }
- }
- class Suitcase extends Container
- {
- function __construct()
- {
- $this->strLuggageType="Suitcase";
- }
- }
- class Bag extends Container
- {
- function __construct()
- {
- $this->strLuggageType="Bag";
- }
- }
- $objNewCar=new Sports(new Bag());
- $objNewCar=new Sports(new Suitcase());
- print_r($objNewCar->arrLuggage);
- ?>
Add Comment
Please, Sign In to add comment