What is Interface?
We all know the PHP class support only single inheritance, Can you achieve multiple inheritance in it? The answer is no. You can achieve multiple inheritance through another specific method which is Interface.
Interfaces allow you to define a some standard structure for your classes.Interface helps to create multiple inheritance because as most of the developers knows, PHP does not support multiple inheritance directly, we need to use interface to implement multiple inheritance which is not supported with abstract.
Abstract and interfaces are more difficult to understand in Object Oriented concept, is relatively easy to confuse.In one of the earlier post,I have explained about Abstract in PHP.
Interfaces in PHP define which methods must be implemented in a class like other object-oriented (OOP) language. Interface contain the name and parameters but no content, the actual code will be written into class.
The interface can be implemented by the class, not extended.The interface can contain the abstract method.if Interface variables are constant or final then it must be initialized.Interfaces may be used on any object.
SYNTAX:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | access_specifier Interfacee interface_name //access_specifier is public or no_modifier { return_type methodname1(arg list); // Method declaration return_type methodname2(arg list); type variable_name1 = value1; // Variable declaration type variable_name2 = value2 ….......... …....... } // Interface created class Class_name implements interface_name{ // Desired method code } } |
NOTE: Method of Interface implemented by class must be Public.
Any class can extend only one other class, but it can implement more than one class.When you have some common methods which will be applicable on many classes, You can move to the interface.Technically, method implementation within the interface will ensure that the function are required.
The general syntax for class is shown below:
SYNTAX:
1 2 3 | access_specifier class Class_name [extends super_class] [implements interface1 [, interface2]...] |
NOTE: Interface can extends other interface.
Can I have an example?
First let’s take a look at what an Interface is.
1 2 3 4 5 6 7 8 9 10 11 12 13 | interface intI{ function disp(); } class Cls implements intI{ public function disp(){ echo "inteface intI implemented by Cls"; } } $obj = new Cls(); $obj->disp(); // prints “inteface intI implemented by Cls” |
I declare a shop interface class defines method disp().disp() method will be inherited in class which implement the interface.Here I have print the message you can use it like a method.
Extended Interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | interface I1{ function m1(); } interface I2 extends I1{ function m2(); } class C1 implements I1{ public function m1(){ echo "Method M1<br/>"; } public function m2(){ echo "Method M2"; } } $obj = new C1(); $obj->m1(); $obj->m2(); //Prints //Method M1 //Method M2 |
Finally, the difference between class and interface
NOTE: A class may not need to implement all the methods defined in an interface, but it must be defined if defined as an abstract.
Here are the couple of articles which you should definitely like:
Static Keyword in PHP
Type Hinting in PHP5
Class
- We can extend the class.
- Multiple inheritance is not supported by class.
Interface
- We can implement the interface.
- Interface can support multiple inheritance.
That’s it. Hopefully, this small look about the interface will help you out.Do let me know if you know other advantages of Interface? What another practical usage of Interface? Share with me on the comment section.
Comments (1)