Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an interface composed of other interfaces?

Tags:

php

interface

I'd like to create an interface, IFoo, that's basically a combination of a custom interface, IBar, and a few native interfaces, ArrayAccess, IteratorAggregate, and Serializable. PHP doesn't seem to allow interfaces that implement other interfaces, as I get the following error when I try:

PHP Parse error: syntax error, unexpected T_IMPLEMENTS, expecting '{' in X on line Y

I know that interfaces can extend other ones, but PHP doesn't allow multiple inheritance and I can't modify native interfaces, so now I'm stuck.

Do I have to duplicate the other interfaces within IFoo, or is there a better way that allows me to reuse the native ones?

like image 217
FtDRbwLXw6 Avatar asked Dec 18 '12 22:12

FtDRbwLXw6


People also ask

Can interface use other interfaces?

An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces.

Can you implement one interface from another?

An interface can extend any number of interfaces but one interface cannot implement another interface, because if any interface is implemented then its methods must be defined and interface never has the definition of any method.

Can an interface implement another interface PHP?

Classes may implement more than one interface if desired by separating each interface with a comma. A class can implement two interfaces which define a method with the same name, only if the method declaration in both interfaces is identical.

How can you extend one interface by the other interface discuss?

Extending Interfaces An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. The following Sports interface is extended by Hockey and Football interfaces.


2 Answers

You are looking for the extends keyword:

Interface IFoo extends IBar, ArrayAccess, IteratorAggregate, Serializable {     ... } 

See Object Interfaces and in specific Example #2 Extendable Interfaces ff.

like image 131
hakre Avatar answered Sep 22 '22 22:09

hakre


You need to use the extends keyword to extend your interface and when you need to implement the interface in your class then you need to use the implements keyword to implement it.

You can use implements over multiple interfaces in you class. If you implement the interface then you need to define the body of all functions, like this...

interface FirstInterface {     function firstInterfaceMethod1();     function firstInterfaceMethod2(); } interface SecondInterface {     function SecondInterfaceMethod1();     function SecondInterfaceMethod2(); } interface PerantInterface extends FirstInterface, SecondInterface {     function perantInterfaceMethod1();     function perantInterfaceMethod2(); }   class Home implements PerantInterface {     function firstInterfaceMethod1()     {         echo "firstInterfaceMethod1 implement";     }      function firstInterfaceMethod2()     {         echo "firstInterfaceMethod2 implement";     }     function SecondInterfaceMethod1()     {         echo "SecondInterfaceMethod1 implement";     }     function SecondInterfaceMethod2()     {         echo "SecondInterfaceMethod2 implement";     }     function perantInterfaceMethod1()     {         echo "perantInterfaceMethod1 implement";     }     function perantInterfaceMethod2()     {         echo "perantInterfaceMethod2 implement";     } }  $obj = new Home(); $obj->firstInterfaceMethod1(); 

and so on... call methods

like image 45
furkanali89 Avatar answered Sep 19 '22 22:09

furkanali89