đ 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:
| Feature | Abstract Class | Interface |
| Instantiation | â Cannot be instantiated | â Cannot be instantiated |
| Methods | Can have both abstract (no body) and concrete (with body) methods | Only abstract methods (no body) |
| Properties | â Can have properties (variables) | â Cannot have properties |
| Access Modifiers | Can be public, protected, or private | Must be public |
| Inheritance | A class can extend only one abstract class | A class can implement multiple interfaces |
| Use Case | When 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:
PaymentGatewayfor PayPal, Stripe, Credit Card.
â Use Abstract Class when:
You want to share common code and also enforce certain rules.
Example:
VehicleprovidesfuelUp()for all vehicles but forces each vehicle to define its ownstartEngine().
â 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."
Comments
Join the discussionNo comments yet. Be the first to comment.