So I have the following interface defined for a car:
interface FuelTank
{
public function useFuel();
}
interface WaterTank
{
public function useWater();
}
interface CarInterface extends FuelTank, WaterTank
{
public function drive($miles);
public function setFuelTank(FuelTank $tank);
public function setWaterTank(WaterTank $tank);
}
I have tried to build a class that implements and complies to the CarInterface
but I'm having trouble with the injection of the FuelTank and Watertank dependancies. This is the class I came up with:
class Car implements CarInterface
{
protected $fuelTank;
protected $waterTank;
public function drive($miles)
{
return $miles;
}
public function useWater()
{
}
public function useFuel()
{
}
public function setFuelTank(FuelTank $tank)
{
$this->fuelTank = $tank;
}
public function setWaterTank(WaterTank $tank)
{
$this->waterTank = $tank;
}
}
Now when I run the following:
$car = new Car;
$car->setFuelTank(new FuelTank);
I get this message:
Fatal error: Cannot instantiate interface FuelTank
I also can't declare the class FuelTank as I get this error instead:
Fatal error: Cannot redeclare class FuelTank
Can someone help me understand how to correctly implement the interface above and what's going on here?