日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php中子类实现多接口,PHP子类无法实现相同的接口父类实现

發布時間:2025/3/15 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php中子类实现多接口,PHP子类无法实现相同的接口父类实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

子類是否無法實現相同的接口父類實現的正常行為?我得到了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中實現了接口藍圖而沒有方法impement_me()為什么子類不能實現相同的接口父類實現?

如果我為第二類實現藍圖以外的其他接口然后它工作,我必須在類2中使用blueprint_new方法,否則觸發致命錯誤.這部分按預期工作.

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.

解決方法:

子類自動繼承父類的所有接口.

有時您不希望這樣,但您仍然可以在子類中實現任何甚至多個接口.

唯一不起作用的是擴展接口,就像無法實現類(或抽象類)一樣.

在第二個代碼中觸發的錯誤是因為你沒有在第二類中實現接口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

標簽:php,oop,interface,extending-classes

來源: https://codeday.me/bug/20190823/1702610.html

總結

以上是生活随笔為你收集整理的php中子类实现多接口,PHP子类无法实现相同的接口父类实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。