Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many interfaces can a class implement in PHP?

I'm looking for an answer to question which is not difficult, but I can't find out how many interfaces can be implemented by one class.

Is this possible?

class Class1 implements Interface1, Interface2, Interface3, Interface4 {    ..... } 

For all the similar examples I found, I've seen that there can be only 2 interfaces implemented by one class. But there isn't any info about what I'm looking for.

like image 621
Arnas Pečelis Avatar asked Feb 11 '15 13:02

Arnas Pečelis


People also ask

Can a class implement multiple interfaces in PHP?

Yes, more than two interfaces can be implemented by a single class. From the PHP manual: Classes may implement more than one interface if desired by separating each interface with a comma.

How many interfaces can a class implement?

A class can implement more than one interface at a time. A class can extend only one class, but implement many interfaces. An interface can extend another interface, in a similar way as a class can extend another class.

Can we implement multiple interfaces in a class?

Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).

How are interfaces implemented in PHP?

PHP - Interfaces vs.All interface methods must be public, while abstract class methods is public or protected. All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary. Classes can implement an interface while inheriting from another class at the same ...


2 Answers

There is no limit on the number of interfaces that you can implement. By definition, you can only extend (inherit) one class.

I would, as a practical matter, limit the number of Interfaces you do implement, lest your class become overly bulky and thus hard to work with.

like image 152
Machavity Avatar answered Sep 28 '22 03:09

Machavity


You can implement as many class you want, there is no limitation in that.

class Class1 implements Interface1, Interface2, Interface3, Interface4, Interface5, Interface6{    ..... }  

This means this is right Hope this helps you

like image 39
Utkarsh Dixit Avatar answered Sep 28 '22 05:09

Utkarsh Dixit