Menu

PHP Abstract Class: Abstract Methods and When to Use

A PHP abstract class is declared abstract class Name and cannot be instantiated: it exists to be extended. It can hold properties, a constructor and finished methods, plus abstract methods with no body that every child class must implement.

This page includes runnable editors - edit, run, and see output instantly.

A PHP abstract class is declared with abstract class Name and cannot be created with new. It is a base for other classes: it can have properties, a constructor and finished methods, plus abstract methods with no body, which every child class must implement.

describe() is written once in Shape and calls area(), which each child supplies. The abstract class guarantees that the method exists, so describe() can rely on it.

You cannot create an abstract class with new

An abstract class is incomplete on purpose: Shape::area() has no body, so a plain Shape object would have nothing to run. PHP refuses to create one and throws an Error:

Abstract methods: the rules

An abstract method has a signature and a semicolon instead of a body. Each child must implement it with a compatible signature and the same or wider visibility: an abstract protected method may become protected or public in the child, never private. A class with even one abstract method must itself be declared abstract.

A child that skips an abstract method stops the script before any of it runs, unless the child is also declared abstract and leaves the method to its own children:

PHP Fatal error:  Class Square contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Shape::area) in /home/index.php on line 3

The template method pattern

The most common reason to write an abstract class: a fixed sequence of steps where a few steps differ. The base class owns the sequence in a final method, and children fill in the steps. Here the same report runs as plain text and as an HTML table:

Run it and the Page tab shows the HTML table above the plain text version. open() and close() have default bodies, so only HtmlReport bothers to override them; row() is abstract because there is no sensible default. Making render() final keeps a child from changing the order of the steps.

An abstract class can implement an interface

An abstract class may declare implements and leave some of the interface's methods for its children. This is the standard way to pair an interface (the type) with a base class (the shared code):

Code type-hints the interface Cache, so a class that cannot extend BaseCache (because it already extends something else) can still implement Cache directly.

When to use an abstract class, and when not to

Reach for an abstract class when the classes in a family share real code: properties, a constructor, methods that are the same for all of them. If the base would contain nothing but abstract methods, it is an interface in disguise, and an interface is the better choice because a class can implement many interfaces but extend only one class. If you only want to share a few helper methods between unrelated classes, use a trait instead of forcing them under one parent.

A quick test before writing abstract class: would every child really be a kind of the base (Circle is a Shape)? If the honest answer is "it can do this" rather than "it is one of these", write an interface.

Frequently Asked Questions

What is an abstract class in PHP?

A class declared with abstract class Name that cannot be created with new. It is a base for other classes: it can contain properties, a constructor and normal methods, and it can declare abstract methods that each child class must implement.

What is an abstract method in PHP?

A method declared with abstract and no body: abstract protected function render(array $rows): string;. A class that contains one must itself be declared abstract, and every non-abstract child class must define a method with a compatible signature.

Can an abstract class have a constructor in PHP?

Yes. You cannot call new on the abstract class, but its constructor runs when a child class is created, either automatically (if the child has no constructor) or through parent::__construct(...).

Can you instantiate an abstract class in PHP?

No. new Shape() on an abstract class throws an Error: Cannot instantiate abstract class Shape. Create an object of a concrete child class instead, for example new Circle(2).

When should I use an abstract class in PHP?

When several related classes share real code (properties, a constructor, common methods) and differ in a few steps. Put the shared code in the abstract class and declare the differing steps abstract. If there is no shared code and you only need a contract, use an interface.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED