Skip to main content

🚀 PHP Interfaces vs Abstract Classes: Explained Simply for Beginners

Published 5 min readMarkdown
🚀 PHP Interfaces vs Abstract Classes: Explained Simply for Beginners

If you’re learning PHP Object-Oriented Programming (OOP), you’ll quickly come across interfaces and abstract classes.

At first, they might sound confusing — but don’t worry! In this guide, we’ll break them down in plain English, with easy examples, so even if you’re just starting out, you’ll understand them clearly.


🔹 Quick OOP Refresher

In OOP, we use classes and objects to organize code.

  • A class is like a blueprint (for example, Car).

  • An object is something made from that blueprint (for example, My red Toyota).

Sometimes, we need rules to make sure different classes all behave the same way. That’s where interfaces and abstract classes come in.


🔹 What is an Interface?

An interface is like a rulebook.

It says:

“Any class that uses me must have these methods.”

But it doesn’t say how the methods should work. That’s up to each class.

👉 Real-life analogy:
Imagine you are signing up as a delivery driver for a food app (like Uber Eats). The company (the interface) says:

  • You must have a vehicle.

  • You must be able to deliver food.

But it doesn’t care how you do it — you could use a bike, a car, or even a scooter.


🛠 Practical Example: Payment System

Let’s say we’re building a payment system that should support multiple payment methods: Credit Card and PayPal.

We want to enforce that every payment method must have two methods: pay() and refund().

Step 1: Define the Interface

<?php
interface PaymentGateway {
    public function pay($amount);
    public function refund($transactionId);
}

Step 2: Implement the Interface in Classes

class CreditCardPayment implements PaymentGateway {
    public function pay($amount) {
        echo "Paying $$amount using Credit Card.<br>";
    }

    public function refund($transactionId) {
        echo "Refunding transaction $transactionId to Credit Card.<br>";
    }
}

class PayPalPayment implements PaymentGateway {
    public function pay($amount) {
        echo "Paying $$amount using PayPal.<br>";
    }

    public function refund($transactionId) {
        echo "Refunding transaction $transactionId via PayPal.<br>";
    }
}

Step 3: Use the Classes

$cc = new CreditCardPayment();
$cc->pay(100);
$cc->refund("TXN12345");

echo "<br>";

$paypal = new PayPalPayment();
$paypal->pay(200);
$paypal->refund("TXN67890");

✅ Output:

Paying $100 using Credit Card.
Refunding transaction TXN12345 to Credit Card.

Paying $200 using PayPal.
Refunding transaction TXN67890 via PayPal.

🔹 Multiple Interfaces in PHP

A class can implement multiple interfaces, which is useful when it needs to follow more than one set of rules.

interface Logger {
    public function log($message);
}

interface Notifier {
    public function sendNotification($message);
}

class UserService implements Logger, Notifier {
    public function log($message) {
        echo "Log: $message<br>";
    }

    public function sendNotification($message) {
        echo "Notification: $message<br>";
    }
}

$user = new UserService();
$user->log("User created");
$user->sendNotification("Welcome new user!");

🔹 What is an Abstract Class in PHP?

An abstract class in PHP is a class that cannot be instantiated directly.
It’s meant to be a base class for other classes.

  • It can have abstract methods (declared but not implemented).

  • It can also have concrete methods (methods with code).

  • Child classes that extend an abstract class must implement all of its abstract methods.

👉 Analogy:
Think of an abstract class as a partially built house. It gives you walls, plumbing, and structure, but leaves certain rooms unfinished for you to complete.


🛠 Example: Vehicle System

We want all vehicles to have a startEngine() method, but some behavior can be common to all vehicles.

Step 1: Define Abstract Class

<?php
abstract class Vehicle {
    abstract public function startEngine(); // must be implemented by children

    public function fuelUp() {
        echo "Filling fuel...<br>";
    }
}

Step 2: Implement Child Classes

class Car extends Vehicle {
    public function startEngine() {
        echo "Car engine started.<br>";
    }
}

class Bike extends Vehicle {
    public function startEngine() {
        echo "Bike engine started.<br>";
    }
}

Step 3: Use the Classes

$car = new Car();
$car->startEngine();   // Car engine started.
$car->fuelUp();        // Filling fuel...

$bike = new Bike();
$bike->startEngine();  // Bike engine started.
$bike->fuelUp();       // Filling fuel...

🔹 Abstract Class vs Interface in PHP

Here’s a side-by-side comparison:

FeatureAbstract ClassInterface
Instantiation❌ Cannot be instantiated❌ Cannot be instantiated
MethodsCan have both abstract (no body) and concrete (with body) methodsOnly abstract methods (no body)
Properties✅ Can have properties (variables)❌ Cannot have properties
Access ModifiersCan be public, protected, or privateMust be public
InheritanceA class can extend only one abstract classA class can implement multiple interfaces
Use CaseWhen classes share common behavior (code reuse + contract)When classes only need to share method signatures (contract only)

🔹 Practical Analogy

  • Interface → A job contract. It lists what you must do but doesn’t show how you should do it.

  • Abstract Class → A partially completed project. Some work is already done, but you need to finish the rest.


🔹 When to Use What?

✅ Use Interface when:

  • You want to enforce that multiple classes share the same method signatures.

  • Example: PaymentGateway for PayPal, Stripe, Credit Card.

✅ Use Abstract Class when:

  • You want to share common code and also enforce certain rules.

  • Example: Vehicle provides fuelUp() for all vehicles but forces each vehicle to define its own startEngine().


✅ Final Summary

  • Interface = “What to do”

  • Abstract Class = “What to do” + “Some ways to do it”

  • Interfaces are best for contracts, abstract classes are best for code reuse + contracts.

  • You can implement multiple interfaces, but only extend one abstract class.


📝 Final Thoughts

Both interfaces and abstract classes are powerful tools in PHP OOP.

  • Use interfaces when you just need to define rules.

  • Use abstract classes when you also want to share some ready-made functionality.

By mastering these two, you’ll be able to write cleaner, more organized, and more scalable PHP applications.

About the author

  • "Passionate web developer specializing in PHP, Laravel, and Filament, dedicated to building elegant, efficient, and scalable web solutions. Experienced in deploying projects and exploring new technologies to bring ideas to life."

No comments yet. Be the first to comment.

More from this blog