我无法弄清楚为什么这段代码在PHP中不起作用?用子接口覆盖方法参数作为新参数
<?php
interface Engine {
function run();
}
interface HydroEngine extends Engine {
function run();
}
interface Car {
function setEngine(Engine $engine);
}
interface WaterCar extends Car {
function setEngine(HydroEngine $engine);
}
?>
它似乎不会破坏任何OOP规则,但它为什么会给我一个错误?
Fatal error: Declaration of WaterCar::setEngine() must be compatible with Car::setEngine(Engine $engine)
可能是我之前误解了LSP ..但我认为Java允许你做这件事(我错了?) – pleerock
不知道。 PHP不。 :)我希望解释为什么对你有意义。 – deceze
是的,谢谢。我在考虑''HydroEngine'确实与* Engine完全相同或更多*然后'Engine' - 这就是为什么它可以接受lsp,但现在您的解释确实有意义 – pleerock