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.
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');
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.
new PaymentGateway(): Instantiating an abstract class throws a fatal Error: Cannot instantiate abstract class. Always instantiate a concrete subclass.{} to an abstract method: Abstract method declarations end with a semicolon ;, never curly braces.Invoice and User both need toCsv(), use an interface or trait instead of forcing a shared parent class.__construct() method?Yes. Child classes inherit the constructor automatically or call parent::__construct(...) to initialize shared protected properties.
private?No. Child classes must be able to see and override abstract methods, so they can only be public or protected.
Yes. An abstract class can leave some interface methods unimplemented, passing that requirement down to its concrete subclasses.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.