PHP Multiple Inheritance with Interfaces

后端 未结 5 1270
醉话见心
醉话见心 2020-12-14 19:39

I\'m trying to understand how using interfaces gives me multiple inheritance as I\'ve been googling.

class A
{
 function do1(){}
 function do2(){}
 function          


        
5条回答
  •  春和景丽
    2020-12-14 19:45

    Multiple inheritance is possible only for Interfaces!

    such as my output for it:

    php > interface A{};
    php > interface B{};
    php > interface C extends A,B{};
    php > class D implements C{};
    php > $d = new D();
    php > echo ($d instanceof A);
    1
    
    • I created A and B interfaces and C interface extends them.
    • After we have D class which implements C interface
    • Finally, I ask if $d object is instanceof A interface, yeah it's true

    For the lulz, I try to create E class which extends D and stdclass classes and get error!

    php > class E extends D, stdclass{};
    PHP Parse error:  syntax error, unexpected ',', expecting '{' in php shell code on line 1
    
    Parse error: syntax error, unexpected ',', expecting '{' in php shell code on line 1
    

提交回复
热议问题