PHP interface vs abstract class: differences and when to use each
Interfaces define what a class can do; abstract classes share how it is done. A side-by-side table, examples of each, the common pattern of using both, and what PHP 8.1 to 8.4 changed for interfaces.
On this page
An interface is a contract: it lists methods a class promises to have, with no implementation. An abstract class is a partly built class: it can hold code and state that subclasses share, and leave some methods for them to finish. Use an interface to say what something can do, and an abstract class to share how it is done.
The differences at a glance
| Interface | Abstract class | |
|---|---|---|
| A class can use | Many (implements A, B, C) |
One (extends Base) |
| Method bodies | No | Yes, plus abstract methods without bodies |
| Properties | Only hooked properties, PHP 8.4+ | Yes, any |
| Constructor | Can declare a signature, rarely useful | Yes, with code |
| Visibility of methods | Always public |
public, protected or private |
| Constants | Yes (overridable unless final, PHP 8.1+) |
Yes |
| Can be instantiated | No | No |
| Enums can use it | Yes | No, enums cannot extend classes |
| Typical name | Cache, Clock, PaymentGateway, …Interface |
AbstractController, BaseRepository |
An interface: a contract with many implementations
interface Cache
{
public function get(string $key, mixed $default = null): mixed;
public function set(string $key, mixed $value, int $ttl = 3600): void;
}
final class RedisCache implements Cache { /* ... */ }
final class ArrayCache implements Cache { /* for tests */ }
final class PriceService
{
public function __construct(private Cache $cache) {}
}
PriceService depends on the contract, not on Redis. In tests you pass ArrayCache; in production, RedisCache. Classes that have nothing else in common can implement the same interface, and one class can implement several (implements Cache, Countable). This is why frameworks type-hint interfaces in constructors, and why PHP-FIG standards are mostly interfaces.
An abstract class: shared code with blanks to fill in
abstract class Importer
{
public function __construct(protected LoggerInterface $logger) {}
// The algorithm is fixed here...
final public function import(string $path): int
{
$count = 0;
foreach ($this->rows($path) as $row) {
$this->save($this->map($row));
$count++;
}
$this->logger->info(static::class . " imported $count rows");
return $count;
}
// ...and subclasses supply the parts that differ.
abstract protected function rows(string $path): iterable;
abstract protected function map(array $row): array;
protected function save(array $record): void { /* shared default */ }
}
final class CsvImporter extends Importer
{
protected function rows(string $path): iterable { /* fgetcsv loop */ }
protected function map(array $row): array { return ['sku' => $row[0], 'price' => (int) $row[1]]; }
}
The abstract class owns the constructor, the logger property and the import() flow. Subclasses only fill in rows() and map(). That is the template method pattern, and it is the main job abstract classes do well. Note the protected methods: interfaces cannot have those.
The usual answer: both
Libraries often ship an interface and an abstract base class that implements it:
interface Notifier { public function send(Message $m): void; }
abstract class AbstractNotifier implements Notifier
{
public function send(Message $m): void
{
if ($this->shouldSkip($m)) return;
$this->deliver($this->format($m));
}
abstract protected function deliver(string $body): void;
protected function format(Message $m): string { return $m->subject . "\n\n" . $m->body; }
protected function shouldSkip(Message $m): bool { return false; }
}
Code that uses notifiers type-hints Notifier. Code that writes a notifier can extend AbstractNotifier to save work, or implement Notifier from scratch if the base class does not fit. Nobody is forced into the inheritance tree.
What about traits?
A trait copies methods into a class, which avoids the single-inheritance limit for sharing code. Traits cannot be type-hinted, so pair them with an interface when callers need a type: class Post implements HasSlug { use GeneratesSlug; }. Prefer composition, injecting a helper object, when the shared code has its own dependencies.
What changed in recent PHP versions
- PHP 8.0: abstract methods in traits can be private and their signatures are checked;
staticis allowed as a return type, handy in fluent interfaces. - PHP 8.1: interface constants can be overridden by implementing classes unless declared
final; enums can implement interfaces;newin initializers. - PHP 8.3:
#[\Override]makes PHP check that a method really overrides one from a parent class or interface, catching typos after a rename. - PHP 8.4: interfaces can declare properties with hooks, such as
public string $name { get; }, so a contract can require a readable property instead of a getter method. Abstract classes can declare abstract hooked properties too.
How to choose
- Will other code depend on it as a type, with several possible implementations? Start with an interface.
- Do the implementations share real code or state? Add an abstract class that implements the interface.
- Is there only ever one implementation? You may need neither. A plain
finalclass is easier to read, and you can extract an interface later when a second implementation appears. - Is the hierarchy getting deep? More than one level of abstract classes is a sign to switch to composition.
Interfaces and abstract classes are loaded like any other class; if PHP cannot find one, check your namespaces and PSR-4 autoloading.