I know with a bat, some of you assume an interface or an abstract, but that only handles SOME situations. Here is an example where they break.
Suppose we have classes that implement the same interface and extend the same base
class car extends fourwheeler implements ipaygas{
protected $tank1;
public function payGas($amount){}
}
class sportscar extends fourwheeler implements ipaygas{
protected $tank1;
protected $tank2;
public function payGas($amount){}
}
interface ipaygas{
function payGas($amount);
}
In some situations, an interface is all you need as you can only execute "payGas ()". But what do you do when you have the conditions for satisfaction.
Example: if, before paying gas, you need to (1) check the type of car, (2) use premium gas for the sports car, and (3) fill the second tank of the sports car.
THIS IS WHAT I WANT TO DO, BUT CANNOT
function pumpAndPay(iPayGas $car){
if(gettype($car) == "car"){
fillTank($car,(car) $car->tank1);
}else{
fillTank($car,(sportscar) $car->tank1);
fillTank($car,(sportscar) $car->tank2);
}
}
How can I do this with real type castings? Is this possible in PHP?
( ):
"" ... , , , , , , , ..
abstract class AVechicle{}
abstract class ACar extends AVechicle{}
abstract class ATruckOrSUV extends AVechicle{}
abstract class ABike extends AVechicle{}
class Car extends ACar{}
class SportsCar extends ACar{}
class SUV extends ATruckOrSUV{}
class Truck extends ATruckOrSUV{}
class Bike extends ABike{}
class Scooter extends ABike{}
class GasStation{
public function cleanVehicle(AVehicle $car){
}
public function pumpAndPay(AVehicle $car){
}
public function fullService(AVehicle $car){
$this->cleanVehicle($car);
$this->pumpAndPay($car);
}
}
...