What is an abstract class in PHP?

What is an abstract class in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What is an abstract class in PHP?

An abstract class in PHP is a blueprint for other classes that cannot be instantiated on its own. It serves as a foundational component in object-oriented design, allowing you to define common properties and methods that child classes must inherit or implement. The primary constraint of an abstract class is that it contains at least one abstract method—a method declared without a body—that forces any subclass to provide a specific implementation.

Consider a scenario where you are building a payment processing system. You have different types of payments, such as credit card transactions and bank transfers. Instead of creating separate interfaces for every single payment type or duplicating logic like processPayment() in every class, you define an abstract class called Payment. This class holds shared state, such as the transaction amount or ID, and declares an abstract method execute():

abstract class Payment {
    protected float $amount;
    protected string $id;

    public function __construct(float $amount, string $id) {
        $this->amount = $amount;
        $this->id = $id;
    }

    // Shared logic that applies to all payment types
    public function validate() {
        if ($this->amount <= 0) {
            throw new InvalidArgumentException("Amount must be positive.");
        }
        return true;
    }

    // This method forces subclasses to define their own behavior
    abstract public function execute();
}

When you extend this class, say for a credit card payment, you cannot simply instantiate the parent. Attempting to create new Payment(100, "tx_123") will throw an error because the child class is required to implement the abstract method. The subclass looks like this:

class CreditCardPayment extends Payment {
    public function execute() {
        // Implementation specific to credit cards
        return "Processing via Stripe...";
    }
}

The technical advantage here lies in code reuse and enforcement of contracts. By moving shared logic into the abstract parent, you reduce duplication across your codebase. If you later realize that all payments need to log the transaction timestamp before execution, you add a concrete method or override the existing one in the parent class rather than touching every child implementation.

However, there are trade-offs to consider. Abstract classes are less flexible than interfaces when it comes to defining multiple inheritance paths for behavior. While an abstract class can contain both concrete methods and abstract methods, interfaces in PHP (prior to 8.1) could only define method signatures. In modern PHP, you can combine interfaces with abstract classes, but the decision depends on whether you need shared state or just a contract of behavior.

A common pitfall is overusing abstract classes to enforce rigid hierarchies that might not exist in reality. If your child classes are unrelated except for sharing a method name, an interface might be a cleaner solution. Abstract classes imply a "is-a" relationship where the child is a specialized version of the parent. Misinterpreting this relationship can lead to confusing class structures that are difficult to maintain.

If you are working within the Laravel ecosystem, abstract classes are frequently used in service providers and repository patterns to standardize how data is fetched or services are registered across your application. For more details on leveraging these patterns effectively within a Laravel project, you might explore resources like https://laravelcompany.com.

Our network