【发布时间】:2013-02-07 07:45:19
【问题描述】:
考虑以下 PHP 接口:
interface Item {
// some methods here
}
interface SuperItem extends Item {
// some extra methods here, not defined in Item
}
interface Collection {
public function add(Item $item);
// more methods here
}
interface SuperCollection extends Collection {
public function add(SuperItem $item);
// more methods here that "override" the Collection methods like "add()" does
}
我正在使用 PHPStorm,当我这样做时,我在 IDE 中收到一个错误,该错误基本上表明 SuperCollection 中的 add() 的定义与它扩展的接口中的定义不兼容 Collection .
在某种程度上,我可以看到这是一个问题,因为该方法的签名与它“覆盖”的签名不匹配完全。但是,我确实觉得这是兼容的,因为SuperItem 扩展了Item,所以我认为add(SuperItem) 与add(Item) 相同。
我很好奇 PHP(5.4 或更高版本)是否支持此功能,也许 IDE 存在无法正确捕获此问题的错误。
【问题讨论】:
-
在 5.3 中,我得到“致命错误:SuperCollection::add() 的声明必须与 Collection::add() 的声明兼容”。所以看起来 PhpStorm 是正确的。为什么您认为 5.4 的行为可能会有所不同?