dtnpzghys01643322 2019-08-13 08:25
浏览 21

接口typehints和继承的奇怪行为[重复]

Here's the situation I have:

interface IFoo {}

class FooImplementation implements IFoo {
    public $someProperty;
}

class SomeParentClass {
    public function someMethod(Ifoo $someParam) {...}
}

class SomeChildClass extends SomeParentClass {
    public function someMethod(FooImplementation $someParam) {
         // do something with someProperty from $someParam
    }
}

When trying to run the someMethod from the SomeChildClass I get a warning saying that

Declaration of SomeChildClass::someMethod should be compatible with SomeParentClass::someMethod

However, I do think they are compatible...

I have a parent class that specifies it needs an object adhering to an interface as parameter, and in it's child class, I'm using a class that implements said interface. Therefor, to me, it is strange that is throws that warning,

While I do understand that when doing a straight up compare in classnames, Ifoo is different than FooImplementation. I feel like what I'm trying to do is correct, unless I'm completely overlooking something here.

EDIT: I understand this is due to PHP not supporting covariance... A follow-up question, in the child class, I need to do something with with a property on the FooImplementation class that is not defined in the interface. How would I best go about doing so?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因