PHP重写子类中的接口签名

PHP重写子类中的接口签名,php,oop,inheritance,abstract-class,signature,Php,Oop,Inheritance,Abstract Class,Signature,我有以下接口/类: 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 $

我有以下接口/类:

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) {}
}
如果引擎是“Part”的子类(我知道它在Java中是可能的),为什么我不能在子签名(混合类)中使用引擎对象

在PHP中实现此功能的正确方法是什么?
Thx

简言之,界面旨在通过为对象设置特定指令来提供这些限制。这样,当检查对象的功能或使用
instanceof
时,您总是可以期望收到指定的内容

没有“适当”的方法来实现您想要做的事情,但是建议的方法是使用接口而不是特定的类定义键入hint

这样,您就可以始终保证所提供对象的可用方法

interface TimeLineInterface { }

class Part implements TimeLineInterface { }

class Engine extends Part { }

interface CarInterface
{
    public function selectTimeLine(TimeLineInterface $object);
}

abstract class Car implements CarInterface { }

class Hybrid extends Car
{
   public function selectTimeLine(TimeLineInterface $object) { }
}
如果要强制接受对象方法的特定类型的对象,则需要像这样检查对象实例

class Hybrid extends Car
{
   public function selectTimeLine(TimeLineInterface $object) 
   {
       if (!$object instanceof Engine) {
            throw new \InvalidArgumentException(__CLASS__ . '::' . __FUNCTION__ . ' expects an instance of Engine. Received ' . get_class($object));
       }
   }
}

简言之,界面旨在通过为对象设置特定的指令来提供这些限制。这样,当检查对象的功能或使用
instanceof
时,您总是可以期望收到指定的内容

没有“适当”的方法来实现您想要做的事情,但是建议的方法是使用接口而不是特定的类定义键入hint

这样,您就可以始终保证所提供对象的可用方法

interface TimeLineInterface { }

class Part implements TimeLineInterface { }

class Engine extends Part { }

interface CarInterface
{
    public function selectTimeLine(TimeLineInterface $object);
}

abstract class Car implements CarInterface { }

class Hybrid extends Car
{
   public function selectTimeLine(TimeLineInterface $object) { }
}
如果要强制接受对象方法的特定类型的对象,则需要像这样检查对象实例

class Hybrid extends Car
{
   public function selectTimeLine(TimeLineInterface $object) 
   {
       if (!$object instanceof Engine) {
            throw new \InvalidArgumentException(__CLASS__ . '::' . __FUNCTION__ . ' expects an instance of Engine. Received ' . get_class($object));
       }
   }
}
是的,PHP很烂。=)

如果我没弄错的话,你需要这样的东西:

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) {}
}
是的,PHP很烂。=)

如果我没弄错的话,你需要这样的东西:

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) {}
}

有几个问题可以解决这个问题。这一个似乎是最有希望的,建议使用instanceof:这里有一个,所以:您最好的选择可能是从参数声明中删除类型,并使用
instanceof
@MontgomeryJean我环顾四周时看到了第二个答案……但是您能详细说明一下“instanceof”吗你能举个例子吗?没关系…看到了。有几个问题可以解决这个问题。这一个似乎是最有希望的,建议使用instanceof:这里有一个,所以:您最好的选择可能是从参数声明中删除类型,并使用
instanceof
@MontgomeryJean我环顾四周时看到了第二个答案……但是您能详细说明一下“instanceof”吗你能举个例子吗?没关系…看到了。因为
Part
实现了
SomeInterface
Engine
扩展了
Part
Engine
对象也将实现
SomeInterface
参见我的示例和测试用例。因为
Part
实现了
SomeInterface
Engine
extends
Part
Engine对象也将实现
SomeInterface
参见我的示例和测试用例。