Content · Programming · PHP

PHP Abstract Classes Explained

Learn how abstract classes work in PHP, define abstract methods, share common functionality, and extend base classes with practical object-oriented programming examples.

2 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When building applications with PHP, you may have several classes that share common functionality but also need their own specific behavior.

For example, a tutorial and a news article might both have a title, but each may generate its summary differently.

Abstract classes help organize this kind of code. They allow you to share existing functionality while requiring child classes to implement certain methods.

In this guide, you'll learn how abstract classes work in PHP and when to use them.

What Is an Abstract Class in PHP?


An abstract class is a class declared using the abstract keyword. It is designed to serve as a parent for other classes.

Unlike a regular class, an abstract class cannot be instantiated directly.

For example:

<?php

abstract class Animal
{
    public function eat(): string
    {
        return "The animal is eating.";
    }
}

You cannot create an object using new Animal() because Animal is abstract.

Instead, you create a child class using extends:

class Dog extends Animal
{
}

$dog = new Dog();

echo $dog->eat();

Output:

The animal is eating.

The Dog class inherits the eat() method and can use it without redefining it.

Understanding Abstract Methods


An abstract class can contain abstract methods. These methods declare what child classes must provide but don't contain an implementation.

For example:

abstract class Animal
{
    abstract public function makeSound(): string;
}

Notice that makeSound() ends with a semicolon instead of containing a method body.

A concrete child class must implement this method:

class Dog extends Animal
{
    public function makeSound(): string
    {
        return "Woof!";
    }
}

If a child class doesn't implement all inherited abstract methods, it must also be declared abstract.

Abstract methods are useful when related classes should provide the same operation but use different logic.

Practical Example: Managing Website Content


Let's create a simple content system containing tutorials and news articles.

Both content types have a title and share a method for retrieving it. However, each type should generate its own summary.

Create a file named content.php:

<?php

declare(strict_types=1);

// Define the abstract parent class.

abstract class ContentItem
{
    public function __construct(
        protected string $title
    ) {
    }

    // Shared method with an implementation.

    public function getTitle(): string
    {
        return $this->title;
    }

    // Required method without an implementation.

    abstract public function getSummary(): string;
}

// Create a tutorial class.

class Tutorial extends ContentItem
{
    public function getSummary(): string
    {
        return "Tutorial: " . $this->getTitle();
    }
}

// Create a news article class.

class NewsArticle extends ContentItem
{
    public function getSummary(): string
    {
        return "News: " . $this->getTitle();
    }
}

// Create objects from the child classes.

$items = [
    new Tutorial("Getting Started with PHP"),
    new NewsArticle("PHP Development Updates")
];

// Display each summary.

foreach ($items as $item) {

    echo "<p>" . htmlspecialchars(
        $item->getSummary(),
        ENT_QUOTES,
        "UTF-8"
    ) . "</p>";
}

Browser output:

Tutorial: Getting Started with PHP

News: PHP Development Updates

How the Example Works

  • ContentItem is the abstract parent class.
  • The constructor initializes the shared $title property.
  • getTitle() is a regular method inherited by both child classes.
  • getSummary() is abstract, so each concrete child class must implement it.
  • Tutorial and NewsArticle provide their own summary formats.

Notice that we never create a ContentItem object directly. Instead, we create objects from its concrete child classes.

This design keeps shared code in one place while allowing different classes to customize their behavior.

Abstract Classes vs Interfaces


Abstract classes and interfaces can both define requirements for other classes, but they serve different purposes.

Feature Abstract Class Interface
Declaration abstract class interface
Relationship extends implements
Regular implemented methods Supported Not supported
Instance properties Supported Not supported
Constructor Supported Not declared as a constructor implementation
Multiple relationships A class extends one parent class A class can implement multiple interfaces

Use an abstract class when related classes need shared data or functionality along with required methods.

Use an interface when different classes need to follow the same contract without requiring a shared parent implementation.

An abstract class can also implement interfaces, combining both approaches.

Important Abstract Class Rules


  • An abstract class cannot be instantiated directly.
  • Abstract classes can contain properties, constructors, regular methods, and abstract methods.
  • Abstract methods cannot have a body and cannot be declared private.
  • A concrete child class must implement all inherited abstract methods with compatible signatures and visibility.
  • An abstract class doesn't have to contain any abstract methods.
  • A PHP class can extend only one parent class, whether that parent is abstract or concrete.

Common Beginner Mistakes


Mistake Correct Approach
Trying to instantiate an abstract class Create an object from a concrete child class.
Adding a method body to an abstract method End the abstract method declaration with a semicolon.
Forgetting to implement an abstract method Implement it or declare the child class abstract.
Using incompatible method declarations Follow PHP's method signature and visibility compatibility rules.
Using an abstract class when no shared implementation is needed Consider whether an interface would be more appropriate.

Key Takeaways


  • Use abstract class to define a base class that cannot be instantiated directly.
  • Use abstract methods to require specific behavior from child classes.
  • Abstract classes can provide shared properties, constructors, and implemented methods.
  • Use extends to inherit from an abstract class.
  • Concrete child classes must implement all inherited abstract methods.
  • Choose abstract classes when related objects need both shared functionality and specialized behavior.

PHP abstract classes provide a practical way to organize related objects without duplicating common code. They are especially useful when building applications with shared structures, such as content management systems, reporting tools, and reusable application components.

Share this resourceSend this page to someone who may find it useful.