PHP子类无法实现相同的接口父类实现

罗曼·托索夫(Roman Toasov)

子类不能实现父接口实现的相同接口是正常行为吗?我有PHP v5.6

interface blueprint {
    public function implement_me();
}

class one implements blueprint {

    public function implement_me() {

    }

}

class two extends one implements blueprint {


}

//no fatal error triggered for class two 

编辑:因此,即使我blueprint在子类中实现了接口two而又没有impement_me()子类无法实现相同接口的方法,即使我在子类中实现了接口上述代码也不会产生任何错误或警告

如果我实现了除blueprint以外的其他接口two则它将起作用,并且我必须blueprint_new在类内使用方法,two否则会引发致命错误。这部分按预期工作。

interface blueprint {
    public function implement_me();
}

class one implements blueprint {

    public function implement_me() {

    }

}


interface blueprint_new {
    public function todo();
}


class two extends one implements blueprint_new {


}

//this will trigger fatal error.
丹尼尔W.

子类自动从父类继承所有接口。

有时您不希望这样,但是您仍然可以在子类中实现任何甚至多个接口。

唯一不起作用的是扩展接口,就像无法实现类(或抽象类)一样。

第二个代码中触发的错误是因为您没有从blueprint_new类中的interface实现所有方法two,但是基本上您的代码中没有错。

例:

class MobilePhone implements GsmSignalPowered {}
class SamsungGalaxy extends MobilePhone implements UsbConnection {}
interface ThunderboltPowered {}
interface GsmSignalPowered {}
interface UsbConnection {}

$s = new SamsungGalaxy();
var_dump($s instanceof GsmSignalPowered); // true
var_dump($s instanceof UsbConnection); // true
var_dump($s instanceof ThunderboltPowered); // false

$m = new MobilePhone();
var_dump($m instanceof GsmSignalPowered); // true
var_dump($m instanceof UsbConnection); // false
var_dump($m instanceof ThunderboltPowered); // false

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章