PHP Interface: Complete Guide with Examples

1. Introduction

An Interface in PHP is similar to a class, but it cannot contain method implementations. Instead, it only contains method declarations. Any class that implements the interface must provide the actual implementation of those methods.

Interfaces are very useful when building large applications because they enforce a consistent structure across different classes.


For example, imagine a system where different classes represent different types of communication such as email, SMS, or notifications. All these classes may need to follow the same structure. Using an interface ensures that every class provides the required methods.

📖
Characteristics of PHP Interfaces:
  • An interface defines method signatures only.
  • Classes that implement an interface must define those methods.
  • Interfaces support multiple inheritance.
  • Interfaces help achieve abstraction and loose coupling.

Interfaces are widely used in modern PHP frameworks and applications to maintain clean and scalable code.

2. What is a PHP Interface?

A PHP Interface is a structure that defines a list of methods that a class must implement.

Syntax of an interface:


interface InterfaceName {
   public function methodName();
}
📖
Key points about interfaces:
  • In an interface, only method declarations are allowed.
  • The class implementing the interface must use exactly the same method signature.
  • All methods defined in the interface must be implemented in the class.
  • A class can implement multiple interfaces.
  • An interface cannot be instantiated, just like an abstract class.

Example declaration:


interface communication {
   public function language();
}

Here, the interface defines a method language(), but it does not provide its implementation.

3. Why Use an Interface?

Interfaces are very important in object-oriented design.

1. Achieve Full Abstraction

Interfaces only contain method declarations, which means they provide complete abstraction

Example:


interface Payment {
   public function pay();
}

The actual implementation is provided by classes.

2. Support Multiple Inheritance

PHP does not support multiple inheritance with classes. However, a class can implement multiple interfaces, allowing similar functionality.

3. Loose Coupling

Interfaces help create loosely coupled code. This means components depend on interfaces rather than concrete classes.

This improves:

  • Code flexibility
  • Maintainability
  • Scalability

4. How to Use an Interface

In PHP, interfaces are implemented using the implements keyword.

Example:


class ClassName implements InterfaceName {
   // implement interface methods
}

Important rules:

  • When a class uses an interface, it must implement all its methods
  • The method names and parameters must match exactly.

Also:

  1. When one interface inherits another interface, the keyword extends is used.

Example:


interface A {}
interface B extends A {}

5. Example of PHP Interface

Let’s understand a simple example.


interface communication {
   public function language();
}

class French implements communication {

   public function language() {
       echo "French person usually speaks French language.";
   }

}

class Indian implements communication {

   public function language() {
       echo "Indian person usually speaks Hindi language.";
   }

}

$fre_obj = new French;
$fre_obj->language();

echo "
"; $ind_obj = new Indian; $ind_obj->language();


Output:

French person usually speaks French language. Indian person usually speaks Hindi language.

Explanation:

  • The communication interface defines a method language().
  • Both French and Indian classes implement the interface.
  • Each class provides its own implementation of the language() method.

This demonstrates how multiple classes can follow the same interface structure.

6. Multiple Inheritance Using Interface

PHP allows a class to implement multiple interfaces.

This concept is known as multiple inheritance using interfaces.

Example:


interface communication {
   public function language();
}

interface country {
   public function people();
}

class Indian implements communication, country {

   public function people() {
       echo "Indian people ";
   }

   public function language() {
       echo "speak usually Hindi language.";
   }

}

$ind_obj = new Indian;

$ind_obj->people();
$ind_obj->language();

Output:

Indian people speak usually Hindi language.

Explanation:

  • The class Indian implements two interfaces:
    • communication
    • country
  • Therefore, the class must implement both methods:
    • language()
    • people()

This allows PHP to simulate multiple inheritance.

7. Multiple Interface Inheritance

Interfaces can also inherit other interfaces. This means one interface can extend another interface.

Example:


interface communication {
   public function language();
}

interface country extends communication {
   public function people();
}

class French implements country {

   public function people() {
       echo "French people ";
   }

   public function language() {
       echo "speak usually French language.";
   }

}

$fre_obj = new French;

$fre_obj->people();
$fre_obj->language();

Output:

French people speak usually French language.

Explanation:

  • The country interface extends communication.
  •  The class implementing country must implement methods from both interfaces.

This allows interface hierarchies and better code organization.

8. Conclusion

The PHP Interface is an important concept in Object-Oriented Programming that helps developers design flexible and scalable applications.

Interfaces define a contract that classes must follow, ensuring consistent method structures across different classes. They help achieve abstraction, loose coupling, and multiple inheritance.

By using interfaces, developers can write cleaner and more maintainable code, which is especially useful in large applications and modern PHP frameworks.

PHP Interface – Interview Questions

Q 1: What is an interface in PHP?
Ans: An interface defines a contract that implementing classes must follow.
Q 2: Can interfaces contain method implementations?
Ans: No, only method declarations.
Q 3: Can a class implement multiple interfaces?
Ans: Yes.
Q 4: What keyword is used to implement an interface?
Ans: implements.
Q 5: Why use interfaces?
Ans: To achieve abstraction and multiple inheritance.

PHP Interface – Objective Questions (MCQs)

Q1. Which keyword defines an interface in PHP?






Q2. Can an interface contain method definitions?






Q3. A class implements an interface using ______ keyword.






Q4. Can a class implement multiple interfaces?






Q5. Interfaces cannot have ______.






Related PHP Tutorials