W3docs

Understanding PHP Object-Oriented Programming: The Concept of Abstract Classes

When it comes to programming in PHP, object-oriented programming (OOP) can be a powerful tool for creating clean, organized, and maintainable code. One

In PHP, object-oriented programming (OOP) helps you write clean, organized, and maintainable code. One important building block of OOP is the abstract class — a class that defines a shared contract for a family of related classes without being usable on its own.

This chapter covers what an abstract class is, how to declare one with the abstract keyword, how abstract methods force child classes to provide an implementation, and when to reach for an abstract class instead of an interface.

What Is an Abstract Class?

An abstract class is a blueprint for creating objects, but it cannot be instantiated on its own. Instead, it serves as a base from which other classes inherit common properties and methods. It can mix two kinds of methods:

  • Concrete methods — fully implemented methods that every child inherits as-is.
  • Abstract methods — methods that declare only a signature (name, visibility, and parameters) with no body. Every non-abstract child class must implement them.

This combination lets the parent enforce what must exist while leaving how it works to each child — the foundation of polymorphism in PHP. If you are new to inheritance, read PHP Inheritance first.

Benefits of Using Abstract Classes

  1. Encapsulation: Abstract classes can encapsulate common properties and methods, making it easier to maintain consistency among related classes.
  2. Code Reusability: The inheritance mechanism in OOP allows developers to create new classes by extending existing classes, including abstract classes. This can reduce the amount of code that needs to be written and increase the efficiency of development.
  3. Improved Design: By using abstract classes, developers can create a clear hierarchy of classes, making it easier to understand the relationships between different parts of the code.
  4. Flexibility: By defining an abstract class, developers can specify what properties and methods should be included in a derived class, but leave the implementation details to the derived class. This provides a degree of flexibility and allows developers to create classes that are customized for specific purposes.

How to Define an Abstract Class in PHP

To create an abstract class in PHP, put the abstract keyword before the class declaration. Mark any method that children must implement with abstract as well — an abstract method ends with a semicolon and has no { } body.

PHP abstract class example

abstract class Shape {
  // Concrete method: shared by every child as-is.
  public function describe(): string {
    return "A shape with area " . $this->getArea();
  }

  // Abstract method: each child must implement it.
  abstract public function getArea(): float;
}

Here Shape is declared abstract, so it cannot be turned into an object directly. It provides a ready-made describe() method for all children, while getArea() is left abstract — every concrete subclass must supply its own version.

Abstract Classes Cannot Be Instantiated

Trying to create an object from an abstract class raises a fatal error:

$shape = new Shape(); // Fatal error: Cannot instantiate abstract class Shape

This is exactly the point: an abstract class is incomplete until a child fills in its abstract methods. The visibility of an abstract method (public, protected) is also enforced — the child cannot make it more restrictive. See PHP Access Modifiers for the visibility rules.

Extending an Abstract Class in PHP

To extend an abstract class, use the extends keyword and implement every abstract method. A child can also add its own constructor and extra members.

PHP extend abstract class

class Rectangle extends Shape {
  public function __construct(
    protected float $width,
    protected float $height
  ) {}

  public function getArea(): float {
    return $this->width * $this->height;
  }
}

class Circle extends Shape {
  public function __construct(protected float $radius) {}

  public function getArea(): float {
    return M_PI * $this->radius ** 2;
  }
}

Both Rectangle and Circle extend Shape and provide their own getArea(). They inherit the concrete describe() method without rewriting it.

What if a child forgets an abstract method?

If a non-abstract child does not implement every abstract method, PHP refuses to compile the file:

class Triangle extends Shape {} // Fatal error: Class Triangle contains 1
                                // abstract method and must therefore be
                                // declared abstract or implement getArea()

The compiler catches the missing method before your code ever runs — that guarantee is what makes abstract classes useful for enforcing a contract.

Using an Abstract Class

You never instantiate the abstract class itself — you instantiate a concrete child and call its methods. Because every child honors the same contract, you can treat them uniformly (polymorphism):

PHP abstract class usage example

$shapes = [
  new Rectangle(10, 5),
  new Circle(3),
];

foreach ($shapes as $shape) {
  echo round($shape->getArea(), 2), "\n";
}
// Output:
// 50
// 28.27

Each object resolves getArea() to its own implementation, while describe() (inherited from Shape) works the same for all of them.

Abstract Class vs. Interface

Abstract classes and interfaces both define a contract, but they differ:

FeatureAbstract classInterface
Can contain implemented methodsYesNo — methods are signatures only (no body)
Can hold properties / stateYesOnly constants
A class can have how manyOne (single inheritance)Many (implements A, B)
Use whenChildren share code and a contractYou only need a contract, possibly across unrelated classes

Reach for an abstract class when related classes share real implementation; reach for an interface when you only want to guarantee a set of methods exists. See PHP Interfaces and PHP Classes and Objects for more.

Conclusion

Abstract classes provide a way to enforce consistency and structure in your PHP code, and they offer numerous benefits such as encapsulation, code reusability, improved design, and flexibility. Whether you are a seasoned PHP developer or just starting out, understanding the concept of abstract classes is an important step in mastering OOP in PHP.

Practice

Practice
What are the characteristics and usage of PHP Abstract Classes?
What are the characteristics and usage of PHP Abstract Classes?
Was this page helpful?