Danny Valariola Danny Valariola - 3 months ago 29
PHP Question

PHP Override Interface signature in child class

I have the following interface / classes:

class Part {}
class Engine extends Part{}

interface CarsInterface {
public function selectTimeLine(Part $object);
}

abstract class Car implements CarsInterface {}

class Hybrid extends Car {
public function selectTimeLine(Engine $object) {}
}


Why can't i use Engine object in the child signature (Hybrid Class) if Engine is sub-class of "Part" (I know its possible in Java...)

What is the proper way to achieve this functionality in PHP?
Thx

Answer

Yeah, PHP is sucks. =)

If I'm not mistaken, you need something like:

interface SomeInterface {
}

class Part implements SomeInterface  {}
class Engine extends Part implements SomeInterface{}

interface CarsInterface {
    public function selectTimeLine(SomeInterface $object);
}

abstract class Car implements CarsInterface {}

class Hybrid extends Car {
    public function selectTimeLine(SomeInterface $object) {}
}
Comments