Back to Top

How to use Interface in PHP

PHP Interface Explained

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:

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{
 	public  function function_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:

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.

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

interface I1{
	 function m1();

}
interface I2 extends I1{
 	function m2();
}
class C1 implements I1{
       public function m1(){
		echo "Method M1
"; } 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

  1. We can extend the class.
  2. Multiple inheritance is not supported by class.

Interface

  1. We can implement the interface.
  2. 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)

  1. Last code block will not give any error if you do not declare the method “m2” in that.

    If you really want to show the concept of extended interface you should implement the interface “I2” instead of “I1”.

    Got the point ???

    Also it would be great if you show the example if multiple inheritance for interface as you have mentioned.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to create Bootstrap Switch using CSS

Posted on 3 years ago

Bhumi

How to upload file using WordPress

Posted on 6 years ago

Bhumi

How to move Modx site to Live server

Posted on 6 years ago

Bhumi

The Readers’ Poll – December 2012

Posted on 6 years ago

Bhumi

How To hide WordPress Toolbar

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi