/////////////////////////////////////////////Task 6 /////////////////////////////////////////
ECHO '
Task 6:
';
//Object class Vehicles have 2 properties( make and model )
class Vehicles{
//The private variables holds the object information locally and
//can then be accessed by using relevant public get and set methods.
private $make, $model;
//The construct method sets the object properties when the object is created.
public function __construct($make, $model){
$this->make = $make;
$this->model = $model;
echo $this->get_Formatted_Description();
}
//Set methods if you want to change the make or model
//you can implement bussiness rules in the set method to prevent data issues.
//like changing the make if the model is already set could cause an invalid vehicle.
public function set_Make($NewMake){
$this->make = $NewMake;
}
public function set_Model($NewModel){
$this->model = $NewModel;
}
//Get methods if you want to Access the make or model
//you can implement bussiness rules in the get method to format the output or do
//Data conversion.
public function get_Make(){
return $this->make;
}
public function get_Model(){
return $this->model;
}
public function get_Formatted_Description(){
$strRet = "The make is $this->make, the model is the $this->model.";
return $strRet;
}
}
//Object class petrol_Vehicles is a subclass of vehicles and inherits from it.
//thus all properties and methods from Vehicles are in petrol_Vehicles;
//Petrol_Vehicles extends the current vehicle class by adding fuel properties(Type,TankSize and FuelInTank)
class petrol_Vehicles extends Vehicles{
private $Type, $TankSize, $FuelInTank;
public function __construct($make, $model, $Type="Unleaded", $TankSize=55, $FuelInTank=0){
$this->Type = $Type;
$this->TankSize = $TankSize;
$this->FuelInTank = $FuelInTank;
parent::__construct($make, $model);
}
//Set methods if you want to change the make or model
//you can implement bussiness rules in the set method to prevent data issues.
//like changing the type to electric can be prevented.
public function set_Type($NewType){
$this->Type = $NewType;
}
public function set_TankSize($NewTankSize){
if ($NewTankSize >=0){
$this->TankSize = $NewTankSize;
}
}
public function set_FuelInTank($NewFuelInTank){
if ($NewFuelInTank >=0){
if ($NewFuelInTank <= $this->TankSize){
$this->FuelInTank = $NewFuelInTank;
}
}
}
//Get methods if you want to Access the make or model
//you can implement bussiness rules in the get method to format the output or do
//Data conversion like change liters to gallons.
public function get_Type(){
return $this->Type;
}
public function get_TankSize(){
return $this->TankSize;
}
public function get_FuelInTank(){
return $this->FuelInTank;
}
}
//This creates a new instance of the Petrol_Vehicles class.
$MyCar = new petrol_Vehicles("Mercedes Benz","500S","Unleaded",60,45);
echo "
";
//ouput the properties of the Petrol_Vehicle instance
echo "The vehicle is using ". $MyCar->get_Type() ." petrol and has ". $MyCar->get_FuelInTank() ." liters in the ". $MyCar->get_TankSize() ." liter tank.";
echo "
";
echo "Owner used the car over the weekend and used 5 liters of fuel.
";
//Change some of the properties
$MyCar->set_FuelInTank($MyCar->get_FuelInTank() - 5);
//ouput the properties of the Petrol_Vehicle instance
echo "The car has ". $MyCar->get_FuelInTank() ." liters of ". $MyCar->get_Type() ." fuel left in the ". $MyCar->get_TankSize() ." liter tank.";