PHP Abstract Class vs Interface: Key Differences
In object-oriented PHP, abstract classes and interfaces are essential tools used to design flexible and reusable code. While both enforce contracts on implementing classes and cannot be instantiated on their own, they serve distinct architectural purposes. This article explains the fundamental differences between an abstract class and an interface in PHP, detailing when to use each with clear, direct comparisons.
Understanding Abstract Classes
An abstract class is a class that cannot be instantiated directly and must be extended by a subclass. It is used to define a common identity and shared behavior among closely related classes (an “is-a” relationship).
Key features of abstract classes include: * Partial
Implementation: They can contain both abstract methods (which
have no body and must be defined by the child class) and concrete
methods (which have a fully written body and are inherited directly). *
Properties (State): They can define properties
(variables) with any visibility modifier (public,
protected, private) to maintain state. *
Single Inheritance: A PHP class can inherit from only
one abstract class at a time.
abstract class Animal {
protected string $name;
public function __construct(string $name) {
$this->name = $name;
}
// Concrete method (shared implementation)
public function eat(): void {
echo $this->name . " is eating.";
}
// Abstract method (must be implemented by child)
abstract public function makeSound(): void;
}Understanding Interfaces
An interface is a blueprint that defines a strict contract of behavior that implementing classes must fulfill. It represents a capability (a “can-do” relationship) rather than an identity.
Key features of interfaces include: * No Implementation: They can only declare method signatures; they cannot contain any method bodies or logic. * No State (Properties): They cannot define instance properties. They can only contain constants. * Public Visibility: All methods declared in an interface must be public. * Multiple Inheritance: A PHP class can implement multiple interfaces, allowing it to take on multiple behaviors.
interface Flyable {
public function fly(): void;
}
interface Runnable {
public function run(): void;
}
// A class can implement multiple interfaces
class Duck implements Flyable, Runnable {
public function fly(): void {
echo "Duck is flying.";
}
public function run(): void {
echo "Duck is running.";
}
}Direct Comparison of Differences
| Feature | Abstract Class | Interface |
|---|---|---|
| Inheritance | Single inheritance (a class can only extend one abstract class). | Multiple inheritance (a class can implement multiple interfaces). |
| Method Implementation | Can have both abstract methods and concrete methods with code. | Can only declare method signatures; no code implementation allowed. |
| Properties (State) | Can declare instance properties with
public, protected, or private
scope. |
Cannot contain properties (only constants are allowed). |
| Method Visibility | Methods can be public or
protected. |
All interface methods must be
public. |
| Core Philosophy | Defines what an object is (shared identity and partial functionality). | Defines what an object can do (external contract and behavior). |
When to Use Which?
Use an abstract class when: * You want to share code
(concrete methods) among several closely related classes. * You need to
define non-public methods or manage object state through properties. *
You expect the inheriting classes to share a common identity (e.g.,
Car and Motorcycle extending
Vehicle).
Use an interface when: * You want to define a
specific contract or behavior that can be implemented by completely
unrelated classes (e.g., Loggable implemented by both a
DatabaseConnection and a User class). * You
want to take advantage of multiple inheritance to give a class multiple
capabilities. * You are designing a public API or plugin architecture
where you only care about the exposed behavior, not how it is
implemented.