Interfaces in PHP

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 interface keyword
  • 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

InterfaceAbstract Class
100% abstractCan have concrete methods
Multiple allowedSingle inheritance
No propertiesCan 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.

Related Tutorials

  • OOP in PHP – Introduction to Object-Oriented Programming

    Introduction As PHP projects become larger, managing code using only procedural programming becomes difficult. Files get longer, functions become harder to track, and updating the project takes more time. This is where Object-Oriented Programming (OOP) becomes important. OOP helps developers organize code in a cleaner and more structured way by grouping related data and functionality…

  • Classes & Objects in PHP

    Introduction In Object-Oriented Programming (OOP), classes and objects are the foundation of everything. Once you understand these two concepts clearly, learning advanced OOP topics like constructors, inheritance, and polymorphism becomes much easier. In simple words, a class is a blueprint, and an object is a real instance created from that blueprint. For example, if “Car”…

  • Constructors & Destructors in PHP

    Introduction In Object-Oriented Programming (OOP), constructors and destructors are special methods that automatically run at specific moments in an object’s lifecycle. A constructor runs when an object is created, while a destructor runs when an object is destroyed. These methods help developers manage object initialization and cleanup efficiently. Instead of manually setting values after creating…

  • Access Modifiers in PHP (public, private, protected)

    Introduction Access modifiers in PHP control how properties and methods can be accessed inside Object-Oriented Programming (OOP). They help define the visibility of class members and protect important data from unwanted access. With access modifiers, you can decide who can access a property, who can call a method, and whether a value should be changed…

  • Inheritance in PHP

    Introduction Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class. In simple words, inheritance helps you reuse existing code instead of rewriting it. This makes your code cleaner, more structured, and easier to maintain. In this tutorial, you will…

  • Encapsulation in PHP

    Introduction Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It refers to the concept of restricting direct access to certain parts of an object and allowing controlled access through methods. In simple words, encapsulation means data hiding. Instead of allowing users to directly access and modify object properties, we protect them using…

Leave a Reply

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