abstract (Classes & Methods)
debt(d1/e2/b2/t4)
Closest to 'caught instantly' (d1). PHP itself throws a fatal error if you try to instantiate an abstract class or fail to implement abstract methods. Additionally, phpstan, psalm, and phpcs all catch misuse patterns like abstract classes without abstract methods. Most errors are compile/parse-time fatal errors, making detection essentially instant.
Closest to 'one-line patch or single-call swap' (e1), +1 because some fixes involve adding/removing the abstract keyword or converting to an interface, which may touch a small handful of files (the class and its subclasses). The quick_fix is straightforward: switch between abstract and interface or add missing implementations. Typically a simple parameterized fix but slightly more than a single line.
Closest to 'minimal commitment' (b1), +1. Abstract classes are a localized design choice that affects only the class hierarchy in question. They apply across all PHP contexts (web, cli, queue-worker) but the structural weight is low — it's a single inheritance point, not a system-defining architecture decision. There's a small ongoing tax in that subclasses must conform, but it's well-contained.
Closest to 'notable trap' (t5), -1. The misconception states developers think abstract classes are 'just documentation hints' when PHP enforces them strictly with fatal errors. This is a real but moderate trap. Common mistakes include assuming abstract methods can have a body, using abstract when interface is more appropriate, and declaring abstract classes without abstract methods. A competent developer from other OOP languages would mostly get it right, but the specific PHP behaviors (e.g., no body on abstract methods, class must be declared abstract if it has abstract methods) can trip people up.
Also Known As
TL;DR
Explanation
The abstract keyword in PHP is used to declare classes and methods that define incomplete behaviour. An abstract class cannot be instantiated and may contain both concrete methods and abstract method declarations. Any class containing at least one abstract method must itself be declared abstract. Abstract methods define a required signature without implementation, forcing subclasses to provide concrete logic. Unlike interfaces, abstract classes can include state and shared behaviour but are limited by single inheritance. PHP 8.0+ also allows abstract methods inside traits, enabling partial contracts in reusable components.
Common Misconception
Why It Matters
Common Mistakes
- Declaring abstract classes without abstract methods — unnecessary restriction.
- Forgetting that a class with abstract methods must itself be abstract.
- Using abstract instead of interface when no shared implementation exists.
- Assuming abstract methods can have a body — they cannot.
Avoid When
- All methods are abstract — use an interface instead.
- You only want to prevent instantiation without defining a contract.
- No shared behaviour or structure exists.
When To Use
- Defining required methods without implementation.
- Providing partial implementation with enforced extension points.
- Building template workflows with override steps.
Code Examples
// ❌ Invalid design — abstract used instead of interface
abstract class Logger {
abstract public function log(string $message): void;
}
abstract class Report {
abstract protected function header(): string;
abstract protected function body(): string;
public function generate(): string {
return $this->header() . $this->body();
}
}
final class HtmlReport extends Report {
protected function header(): string {
return '<h1>Report</h1>';
}
protected function body(): string {
return '<p>Content</p>';
}
}