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:

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:

NOTE: Interface can extends other interface.

Can I have an example?

First let’s take a look at what an Interface is.

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

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

Understanding CSS3 Pseudo-elements

Posted on 9 years ago

Bhumi

HTML5 Canvas Element Tutorial

Posted on 9 years ago

Bhumi

How to Get local time using EarthTool

Posted on 11 years ago

Bhumi

Introduction to PDO in PHP

Posted on 9 years ago

Bhumi