What is an Interface in PHP (and Magento), and Why Do We Need It?

If you’re learning object-oriented programming, you’ve probably heard of interfaces. And maybe you’ve wondered:

“Why do we need interfaces when we can simply write classes?”

It’s a fair question. At first, interfaces seem like an extra layer of complexity. But once you understand them, you’ll see they solve real-world problems. Let’s break it down in human-friendly language.


What is an Interface?

Think of an interface as a contract. It doesn’t do the work itself — it just defines what methods a class must have.

In PHP, an interface looks like this:

interface PaymentGatewayInterface
{
    public function pay($amount);
}

This doesn’t contain any logic — just the method signature.

Now, any class that implements this interface must provide the actual logic:

class PaypalGateway implements PaymentGatewayInterface
{
    public function pay($amount)
    {
        echo "Paid $amount using PayPal";
    }
}

class StripeGateway implements PaymentGatewayInterface
{
    public function pay($amount)
    {
        echo "Paid $amount using Stripe";
    }
}

Why Not Just Use Classes Directly?

Good question! Let’s imagine a scenario.

You’re building a Magento store. Today, you only support PayPal payments. So you write:

class PaypalGateway
{
    public function pay($amount)
    {
        echo "Paid $amount using PayPal";
    }
}

Looks fine, right? But next month, your client says:

“We also need Stripe and Razorpay.”

Now, you either rewrite your code or start duplicating logic. The problem grows as your project grows.


How Interfaces Help

With an interface, you don’t care about how the payment happens. You only care that every gateway has a pay() method.

Your checkout code can now depend on the interface instead of a specific class:

function checkout(PaymentGatewayInterface $gateway, $amount)
{
    $gateway->pay($amount);
}

Now you can pass any payment class:

checkout(new PaypalGateway(), 100);
checkout(new StripeGateway(), 200);

Your code doesn’t break, no matter how many gateways you add. This is called dependency inversion (a fancy term that means “depend on contracts, not implementations”).

Real-Life Analogy

Think of a wall socket.

  • The socket is the interface. It defines the shape and expectation: “I provide power.”
  • The appliances (fan, TV, laptop charger) are the classes. Each one plugs in differently inside, but as long as they fit the socket, they work.

Without interfaces, every time you buy a new appliance, you’d have to rewire your house. With interfaces, you just plug and play.


Benefits of Using Interfaces

  1. Flexibility → You can swap implementations without changing the rest of your code.
  2. Testability → In unit tests, you can mock an interface easily.
  3. Scalability → Large projects (like Magento) rely on contracts between modules.
  4. Clean Code → Keeps logic decoupled and easier to maintain.

Interfaces in Magento

Magento uses interfaces everywhere. For example:

Magento\Catalog\Api\ProductRepositoryInterface

This defines how products should be fetched and saved. But behind the scenes, Magento can use different implementations — you don’t care which one, as long as it follows the contract.

That’s why when you inject ProductRepositoryInterface into your code, it just works, no matter how Magento decides to implement it.