I was wondering if it is possible to use interface parameters in PHP functions / methods.
I’m used to coding in .Net and using .Net this is possible, such as having the following interface:
interface IVehicleDataProvider
{
public void Create(IVehicle Vehicle);
}
Then I could implement this in a class as such:
class CarDataProvider : IVehicleDataProvider
{
public void Create(Car Car)
{
//do something
}
}
or
class TruckDataProvider : IVehicleDataProvider
{
public void Create(Truck Truck)
{
//do something
}
}
as long as Car or Truck implements an IVehicle interface.
Can the same thing be done in PHP?
No, you can’t define one type in the interface, and a different one (even though it’s a subclass) in the implementor, the implementor must follow the interface completely.
But even if you define
You can still pass in
Cars andTrucks.