php中子类实现多接口,PHP子类无法实现相同的接口父类实现
子類是否無法實(shí)現(xiàn)相同的接口父類實(shí)現(xiàn)的正常行為?我得到了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
編輯:所以上面的代碼工作很好沒有錯誤或警告即使我在子類2中實(shí)現(xiàn)了接口藍(lán)圖而沒有方法impement_me()為什么子類不能實(shí)現(xiàn)相同的接口父類實(shí)現(xiàn)?
如果我為第二類實(shí)現(xiàn)藍(lán)圖以外的其他接口然后它工作,我必須在類2中使用blueprint_new方法,否則觸發(fā)致命錯誤.這部分按預(yù)期工作.
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.
解決方法:
子類自動繼承父類的所有接口.
有時您不希望這樣,但您仍然可以在子類中實(shí)現(xiàn)任何甚至多個接口.
唯一不起作用的是擴(kuò)展接口,就像無法實(shí)現(xiàn)類(或抽象類)一樣.
在第二個代碼中觸發(fā)的錯誤是因?yàn)槟銢]有在第二類中實(shí)現(xiàn)接口blueprint_new中的所有方法,但基本上你的代碼沒有任何問題.
例:
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
標(biāo)簽:php,oop,interface,extending-classes
來源: https://codeday.me/bug/20190823/1702610.html
總結(jié)
以上是生活随笔為你收集整理的php中子类实现多接口,PHP子类无法实现相同的接口父类实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery 队列
- 下一篇: SageMath安装及使用