Lesson

PHP Abstract Classes

10 min reading
Free Course

PHP Abstract Classes: Sharing Code While Enforcing Contracts

A PHP abstract class is a base class marked with the abstract keyword that cannot be instantiated directly with new. It lets you combine concrete shared methods (and properties) with abstract method signatures that every child class must implement.

How Abstract Methods Work in PHP

Whenever a class declares even one abstract method, the class itself must be marked abstract. Child classes extending it must implement every abstract method with matching parameter types and return type.

<?php
declare(strict_types=1);

abstract class PaymentGateway
{
    public function __construct(protected string $currency = 'USD') {}

    // Concrete shared helper inherited by all gateways
    public function formatAmount(int $cents): string
    {
        return $this->currency . ' ' . number_format($cents / 100, 2);
    }

    // Subclasses must implement this exact contract
    abstract public function charge(int $cents, string $token): bool;
}

class StripeGateway extends PaymentGateway
{
    public function charge(int $cents, string $token): bool
    {
        echo "Charging {$this->formatAmount($cents)} via Stripe ({$token})" . PHP_EOL;
        return true;
    }
}

$gateway = new StripeGateway('USD');
$gateway->charge(2599, 'tok_demo_123');

Abstract Classes vs Interfaces

Choose an abstract class when subclasses share a clear "is-a" hierarchy and need common properties, constructors, or protected helpers. Choose an interface when unrelated classes only need to agree on a shared set of public method signatures.

Common Mistakes to Avoid

  • Trying to run new PaymentGateway(): Instantiating an abstract class throws a fatal Error: Cannot instantiate abstract class. Always instantiate a concrete subclass.
  • Adding a body {} to an abstract method: Abstract method declarations end with a semicolon ;, never curly braces.
  • Using abstract classes for unrelated capabilities: If Invoice and User both need toCsv(), use an interface or trait instead of forcing a shared parent class.

Frequently Asked Questions

Can an abstract class have a __construct() method?

Yes. Child classes inherit the constructor automatically or call parent::__construct(...) to initialize shared protected properties.

Can an abstract method be private?

No. Child classes must be able to see and override abstract methods, so they can only be public or protected.

Can an abstract class implement an interface without defining all interface methods?

Yes. An abstract class can leave some interface methods unimplemented, passing that requirement down to its concrete subclasses.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
PHP Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum