What is the point of interfaces in PHP?

What is the point of interfaces in PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

What is the point of interfaces in PHP?

When you first encounter object-oriented programming in PHP, the distinction between abstract classes and interfaces can feel arbitrary. You are told that both mechanisms allow you to define a set of methods that a specific class must implement. However, the source material highlights a critical limitation: while an Abstract class lets you provide default implementations for those methods, an Interface restricts you strictly to signatures—you cannot add any code to the method bodies.

If your goal is simply to enforce a contract where a class must have a specific function signature, why not just use an abstract class? The answer lies in the flexibility of composition versus inheritance, and how this design choice impacts the architecture of your application.

Decoupling Implementation Details

The primary utility of interfaces in PHP is the ability to decouple the definition of behavior from its implementation without forcing a specific hierarchy. If you define an interface LoggerInterface with methods like logMessage(), any class can implement it regardless of what it inherits from. This means your FileLogger, DatabaseLogger, or even a third-party library's logger can all adhere to the same contract without needing to inherit from a common base class that might carry unnecessary baggage.

Consider a scenario where you are building a payment gateway system. You might define an interface called PaymentProviderInterface with methods like processPayment(). You could then implement this interface using classes representing Stripe, PayPal, or Braintree. If you relied solely on an abstract class, all these providers would likely have to inherit from an AbstractPaymentProvider. This creates a rigid inheritance tree where every new provider must extend that specific base class, potentially inheriting properties or methods that do not belong in a payment context.

The Flexibility of Multiple Inheritance

In C++ and Java, interfaces (or abstract classes in Java) solve the problem of multiple inheritance constraints. PHP supports single inheritance for classes; a class can only extend one parent. However, it allows a class to implement multiple interfaces. This capability is often described as "multiple inheritance without the headaches."

Imagine you are building a service that needs to be both Serializable and Loggable.

interface Serializable {
    public function serialize(): string;
    public function unserialize(string $data): void;
}

interface Loggable {
    public function logContext(): array;
}

class User implements Serializable, Loggable {
    // Implementation goes here
}

If you tried to achieve this using abstract classes only, you would be forced to create a SerializableAndLoggable class that extends both concepts, or duplicate the logic in multiple parent classes. With interfaces, the User class remains clean and focused solely on its own implementation details while satisfying multiple distinct contracts independently.

Abstract Classes for Shared State

While interfaces are excellent for defining behavior contracts, abstract classes remain superior when you need to share state or default logic across a hierarchy of classes. The source material notes that abstract classes allow you to add code to the methods. This is where they shine: when you have a base class that handles common concerns like connection management, error handling, or shared utility functions.

abstract class DatabaseConnection {
    protected $connection;

    public function __construct() {
        // Shared connection logic
    }

    abstract public function query(string $sql): mixed;
    
    // You can provide a default implementation for some methods
    public function commit(): bool {
        return $this->execute('COMMIT');
    }
}

In this example, subclasses of DatabaseConnection inherit the connection state and the default commit logic but are forced to implement the specific query handling. This hybrid approach—using an abstract class for shared state and interfaces for strict behavioral contracts—is a powerful pattern in PHP development.

Practical Application in Modern Frameworks

Frameworks like Laravel leverage these concepts extensively. When you extend Illuminate\Database\Eloquent\Model, you are relying on an abstract class that provides default behavior for relationships, accessors, mutators, and event handling. However, if you need to ensure a specific service adheres to a strict protocol without inheriting its internal logic, you define it as an interface.

The decision to use one over the other often comes down to whether you are defining what a class does (interface) versus providing how it does something shared with siblings (abstract class). Understanding this distinction prevents over-engineering your inheritance hierarchy and keeps your codebase maintainable and testable.

Our network