Php 重写使用类型暗示的接口父方法

Php 重写使用类型暗示的接口父方法,php,interface,type-hinting,Php,Interface,Type Hinting,假设我的老板有一个类和接口: class HisBar {} interface HisFooInterface { public function doSomethingWithBar(HisBar $bar); } 我有自己的类,可以扩展也可以不扩展HisBar class MyBar extends HisBar {} 我希望我能做到这一点: interface MyFooInterface extends HisFooInterface { public funct

假设我的老板有一个类和接口:

class HisBar {}

interface HisFooInterface
{
    public function doSomethingWithBar(HisBar $bar);
}
我有自己的类,可以扩展也可以不扩展
HisBar

class MyBar extends HisBar {}
我希望我能做到这一点:

interface MyFooInterface extends HisFooInterface
{
    public function doSomethingWithBar(MyBar $bar);
}
但是,是的,我得到了错误:

致命错误:必须声明MyFooInterface::doSomethingWithBar() 与中的HisFooInterface::doSomethingWithBar(HisBar$bar)兼容 第xx行的路径\到\MyFooInterface.php


我的意思是,我需要使用一个接口实例,但有不同类型的暗示,这是可能的吗?有没有办法做到这一点?如何更改参数的类型?谢谢。

请参见此处:否-您通过使方法更明确来更改方法,这打破了
HisFooInterface
的合同。理论上,你可以只接受stdClass,也可以不使用任何类型提示,但这与目的背道而驰。顺便说一句,@GeorgeBrighton的可能副本,我使用了
不使用任何类型提示,但仍然是一样的。所以,我对这种接口无能为力?类型提示使接口不灵活/不可更改?如果在子接口中重新定义接受的类型,则破坏了在父接口中声明的类型。这只是PHP在这里向您指出的一个逻辑问题。这不是PHP缺乏灵活性的问题,而是你犯了一个逻辑错误却没有看到它的问题。要解决这个问题,您必须重新定义整个继承。