Understanding Interface Classes in PHP

Published on
-
3 mins read
Authors
Understanding Abstract Classes in PHP

Interface in PHP

In PHP, an interface is defined using the interface keyword. All methods declared inside an interface are inherently abstract and must be public.

Unlike regular classes, interfaces only define method signatures—meaning they specify method names and parameters without providing any implementation.

Here’s a simple example:

<?php
interface Payment
{
public function send();
}

At this point, you might ask:

Why do we need interfaces in the first place? What problem do they solve?

Let’s walk through a real-world scenario.


Without Interface: The Problem

Consider the following implementation:

<?php
class PayWithBankA
{
public function send($pay)
{
var_dump('Processing payment via Bank A: ' . $pay);
}
}
class PayWithBankB
{
public function send($pay)
{
var_dump('Processing payment via Bank B: ' . $pay);
}
}
class OrderController
{
protected $payment;
public function __construct(PayWithBankA $payment)
{
$this->payment = $payment;
}
public function store()
{
$pay = 10000;
$this->payment->send($pay);
}
}
$controller = new OrderController(new PayWithBankA);
$controller->store();

This works fine—until you want to switch from PayWithBankA to PayWithBankB.

To make that change, you’d need to modify the constructor:

public function __construct(PayWithBankB $payment)

This might seem trivial in a small project, but in a larger codebase, this kind of hard dependency can become a maintenance nightmare.

You’d have to update multiple places manually, increasing the risk of bugs.


Using Interface: A Better Approach

Now let’s refactor the code using an interface:

<?php
interface Payment
{
public function send($pay);
}
class PayWithBankA implements Payment
{
public function send($pay)
{
var_dump('Processing payment via Bank A: ' . $pay);
}
}
class PayWithBankB implements Payment
{
public function send($pay)
{
var_dump('Processing payment via Bank B: ' . $pay);
}
}
class OrderController
{
protected $payment;
public function __construct(Payment $payment)
{
$this->payment = $payment;
}
public function store()
{
$pay = 10000;
$this->payment->send($pay);
}
}
$controller = new OrderController(new PayWithBankA);
$controller->store();
$controller = new OrderController(new PayWithBankB);
$controller->store();

Why This Is Better

By introducing the Payment interface, we:

  • Decouple the code from specific implementations (BankA, BankB)
  • Make the system flexible — we can swap implementations easily
  • Avoid modifying existing code when adding new payment methods
  • Follow best practices like Dependency Injection and the Open/Closed Principle

Now, switching payment methods is as simple as changing one line:

$controller = new OrderController(new PayWithBankB);

No need to touch the OrderController at all.


When Should You Use Interfaces?

Interfaces are especially useful when:

  • You have multiple classes that should follow the same contract
  • You want to enforce consistency across implementations
  • You need flexibility to swap implementations without changing core logic
  • You’re building scalable systems (e.g., payment gateways, notification services, APIs)

Final Thoughts

Interfaces are one of the key building blocks in object-oriented programming. They help you write cleaner, more modular, and maintainable code.

If you ever find yourself tightly coupling a class to a specific implementation, it’s usually a good sign that an interface could help.

Start small, and over time, you’ll naturally see where interfaces fit best in your architecture.

That’s all. I hope this was helpful. See you later.