Abstract Class vs Interface in PHP
In this lesson: Difference between abstract class and interface in PHP — rules, comparison table and real examples to choose the right one.
Defining a contract
Sometimes you want to say "any class of this kind must have these methods". PHP offers two tools for this: an abstract class and an interface. Both set rules that other classes must follow, but they differ in how much they provide. Choosing the right one keeps your code well-organised.
Abstract class
An abstract class can have real methods (with code) and abstract methods (just the name). You cannot create an object from it directly — a child class fills in the blanks.
<?php
abstract class Shape {
abstract public function area(); // child must define
public function describe() { // shared code
return "Area is " . $this->area();
}
}
class Square extends Shape {
public $side = 4;
public function area() { return $this->side * $this->side; }
}
echo (new Square())->describe();
?>
Square must define area(), but reuses the finished describe() from the abstract base.
Interface
An interface only lists method names — no code at all. A class implements it and must provide every method.
<?php
interface Printable {
public function printMe();
}
class Invoice implements Printable {
public function printMe() { return "Printing invoice"; }
}
echo (new Invoice())->printMe();
?>
The interface guarantees Invoice has a printMe() method, but provides no code itself. A class can implement many interfaces.
Key differences
| Point | Abstract class | Interface |
|---|---|---|
| Method code | Can have some | None (just names) |
| How many per class | Extend only one | Implement many |
| Keyword | extends | implements |
| Best for | Shared base + blanks | Pure contract |
When to use which
Use an abstract class when related classes share code and each must fill in a few methods — like different shapes that all describe themselves but compute area differently. Use an interface when unrelated classes just need to support the same action — like anything that can be "printed" or "saved", with no shared code.
Common mistakes
- Trying to create an object of an abstract class. You must extend it with a child class first.
- Putting code in an interface. Interfaces only declare method names, not bodies.
- Extending several classes. A class extends only one; use interfaces for multiple contracts.
- Not implementing every interface method. A class must define all methods the interface lists.
Summary
- Both set rules other classes must follow.
- An abstract class can share code and has abstract methods to fill in.
- An interface only lists method names, no code.
- Extend one abstract class; implement many interfaces.
- Abstract = shared base; interface = pure contract.
Next, reuse code across classes with PHP traits, or review the basics in PHP OOP.
Frequently Asked Questions
What is the difference between an abstract class and an interface?
When should I use an abstract class?
When should I use an interface?
Can a class use both an abstract class and interfaces?
extends one abstract class and implements several interfaces at the same time. This combines shared base code with multiple contracts.