Introduction
Interfaces are an important concept in Object-Oriented Programming (OOP) in PHP. They are used to define a contract that classes must follow.
An interface specifies what methods a class must implement, but it does not define how those methods should work.
This helps developers create consistent structures in large applications. Interfaces are widely used in modern PHP frameworks and software architecture.
In this tutorial, you will learn what interfaces are, how they work in PHP, and when you should use them in real projects.
What is an Interface?
An interface defines what a class must do, but not how it does it.
It is a 100% abstract blueprint.
In simple terms:
Interface forces a contract on a class.
Why Use Interfaces?
Interfaces help you:
- Enforce consistency across classes
- Achieve multiple inheritance
- Write flexible and scalable code
- Reduce tight coupling
Interface Rules in PHP
- Defined using
interfacekeyword - All methods are public
- Methods cannot have body
- A class implements interface using
implements - A class must implement all methods
Interface Example
<?php
interface PaymentInterface {
public function pay($amount);
}
?>
Implementing an Interface
<?php
class CardPayment implements PaymentInterface {
public function pay($amount) {
echo "Paid ₹$amount using Card";
}
}
?>
Another Implementation
<?php
class UpiPayment implements PaymentInterface {
public function pay($amount) {
echo "Paid ₹$amount using UPI";
}
}
?>
Using Interface (Polymorphism)
<?php
function makePayment(PaymentInterface $payment, $amount) {
$payment->pay($amount);
}
makePayment(new CardPayment(), 1000);
makePayment(new UpiPayment(), 500);
?>
✔ Same function
✔ Different behavior
✔ Loose coupling
Multiple Interfaces
A class can implement multiple interfaces.
<?php
interface Login {
public function login();
}
interface Logout {
public function logout();
}
class User implements Login, Logout {
public function login() {
echo "User logged in";
}
public function logout() {
echo "User logged out";
}
}
?>
Interface vs Abstract Class
| Interface | Abstract Class |
|---|---|
| 100% abstract | Can have concrete methods |
| Multiple allowed | Single inheritance |
| No properties | Can have properties |
Real-World Example
- Interface →
Logger - Classes →
FileLogger,DatabaseLogger
Switch logging system without changing core code.
When to Use Interface?
✔ When behavior matters, not implementation
✔ When multiple inheritance is needed
✔ When designing APIs or frameworks
Rules of Interfaces in PHP
There are some important rules when working with interfaces:
- All methods inside an interface must be public
- Interfaces cannot contain method bodies
- A class must implement all methods defined in the interface
- Interfaces cannot be instantiated directly
- A class can implement multiple interfaces
Common Mistakes
❌ Trying to add method body
❌ Forgetting to implement all methods
❌ Using interface like a class
Practical Task
- Create an interface called Shape.
Inside it define the method:
calculateArea() - Then create two classes:
- Circle
- Rectangle
- Both classes should implement the interface and define the
calculateArea()method. - Create objects of both classes and display their calculated areas.
Summary
- Interfaces define a contract that classes must follow. They help maintain a consistent structure in applications.
- In PHP, interfaces contain only method declarations and do not include method bodies. Any class implementing an interface must provide the implementation for all its methods.
- Interfaces are useful when designing scalable applications and enforcing common behavior across multiple classes.
- They are widely used in frameworks, libraries, and large PHP projects.
In the next tutorial, we’ll learn about Traits in PHP.
