← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Abstract Classes

php PHP 5.0+ Intermediate
debt(d6/e5/b6/t5)
d6 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5), +1. PHPStan and Psalm can detect some anti-patterns like abstract classes with no abstract methods, but the deeper misuses — choosing abstract class over interface, deep hierarchies, god base classes, LSP violations — require careful code review and architectural judgment. The tools catch surface-level patterns but not the design-level misuse, pushing this above d5.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix says 'prefer interfaces,' but replacing an abstract class with an interface requires modifying every subclass to implement methods that were previously inherited, moving shared code to traits or composition, and updating type hints. This typically spans multiple files within a component. Deep hierarchies (a listed common mistake) make remediation even costlier, but a typical single-level misuse is an e5.

b6 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5), +1. Abstract classes apply across all PHP contexts (web, cli, queue-worker) and tagged with oop/inheritance/solid. A misused abstract class becomes load-bearing: subclasses depend on it, it shapes how new features are added, and it constrains flexibility. It doesn't quite define the system's shape (b9) but exerts strong gravitational pull on the components that inherit from it, affecting many work streams when the hierarchy grows.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception — 'abstract classes are just interfaces with code' — is a well-documented gotcha that most PHP developers eventually learn but initially get wrong. It leads to choosing abstract classes when interfaces suffice, creating unnecessary coupling and rigidity. It's a genuine conceptual trap but not catastrophic since the difference from interfaces is documented and widely discussed, making it a t5 rather than t7.

About DEBT scoring →

Also Known As

abstract class base class

TL;DR

Non-instantiable classes that define shared behaviour and enforce subclasses to implement required methods.

Explanation

Abstract classes define a partial implementation for a group of related classes while enforcing specific methods via abstract declarations. They can include properties, constructors, visibility modifiers, and fully implemented methods. Subclasses extend the abstract class and must implement all abstract methods. Abstract classes are best suited for modelling an 'is-a' relationship with shared behaviour, often used in the Template Method pattern where the algorithm structure is fixed but steps are customisable. Unlike interfaces, they support state and implementation, but PHP allows only single inheritance, which limits flexibility.

Common Misconception

Abstract classes are just interfaces with code. In reality, they introduce inheritance constraints and state, which significantly impacts flexibility and design decisions.

Why It Matters

Using abstract classes correctly prevents duplication while preserving structure — misuse leads to rigid hierarchies, tight coupling, and fragile codebases.

Common Mistakes

  • Using abstract classes when only a contract is needed — prefer interfaces.
  • Creating deep inheritance hierarchies — increases fragility and reduces flexibility.
  • Embedding business logic instead of reusable structure — leads to god base classes.
  • Violating Liskov Substitution Principle in subclasses — breaking expected behaviour.

Avoid When

  • You only need a contract — use interfaces instead.
  • Classes are unrelated but share behaviour — use traits or composition.
  • You anticipate needing multiple inheritance — PHP does not support it.
  • The base class has no real shared implementation.
  • The hierarchy is expected to grow deep or frequently change.

When To Use

  • Subclasses share meaningful implementation and structure.
  • You need to enforce specific steps while allowing custom behaviour.
  • Implementing Template Method pattern.
  • Building framework extension points with controlled lifecycle.
  • Clear is-a relationship exists between base and subclasses.

Code Examples

💡 Note
Abstract class defines the workflow while subclasses implement specific steps (Template Method pattern).
✗ Vulnerable
// ❌ Concrete base class instead of abstract — allows invalid usage
class Animal {
    public function speak(): string {
        return ''; // invalid default
    }
}

$animal = new Animal(); // meaningless instance
✓ Fixed
abstract class PaymentGateway {
    public function charge(Order $order): ChargeResult {
        $this->validate($order);

        $result = $this->processPayment($order); // abstract step

        $this->logCharge($order, $result);
        return $result;
    }

    abstract protected function processPayment(Order $order): ChargeResult;

    protected function validate(Order $order): void {
        if ($order->total <= 0) {
            throw new \InvalidArgumentException('Order total must be positive');
        }
    }

    private function logCharge(Order $order, ChargeResult $result): void {
        // logging implementation
    }
}

final class StripeGateway extends PaymentGateway {
    protected function processPayment(Order $order): ChargeResult {
        // Stripe SDK integration
    }
}

Added 15 Mar 2026
Edited 27 Mar 2026
Views 91
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 2 pings T 2 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 2 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 19 Perplexity 16 Unknown AI 4 ChatGPT 3 Ahrefs 3 Majestic 2 Google 2 SEMrush 2 Claude 1 Meta AI 1
crawler 49 crawler_json 2 rag 1 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Use abstract class only when you need shared implementation + enforced methods; otherwise prefer interfaces.
📦 Applies To
PHP 5.0+ web cli queue-worker
🔍 Detection Hints
abstract class with no abstract methods OR abstract class used only as type hint
Auto-detectable: ✗ No phpstan psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant