PHP abstract Keyword
last modified November 25, 2026
The PHP abstract keyword declares a class that cannot be instantiated and methods that subclasses must implement. It lets a base class own shared state and finished code while leaving the varying parts to its children.
This article shows abstract methods in use, concrete methods beside them, the template method pattern, abstract hierarchies, constructors in abstract classes, and the errors the engine and the compiler raise when the contract is broken.
Basic definitions
An abstract class is declared with the abstract keyword and cannot be instantiated. It exists to be extended, and it usually holds the state and the shared behaviour of a group of related classes.
An abstract method declares only a signature and ends with a semicolon. It has no body, and every concrete class that extends the abstract class must provide one. The signature is a contract: the child must keep the method name and a compatible signature, and it may widen the visibility.
A class may be marked abstract even when it declares no abstract method; the modifier alone is enough to prevent new. The reverse is not allowed: a class that declares an abstract method must itself be abstract, and an abstract method cannot be private, because a private method is invisible to the subclasses that have to implement it.
Abstract classes sit between plain inheritance and interfaces. An interface, covered in PHP interfaces, declares methods without any state; an abstract class can carry properties, constructors, and finished methods, so subclasses inherit code as well as a contract.
An abstract method
The first example declares a shape with one abstract method and one concrete subclass.
<?php
declare(strict_types=1);
abstract class Shape
{
abstract public function area(): float;
}
class Rectangle extends Shape
{
public function __construct(
private float $width,
private float $height
) {
}
public function area(): float
{
return $this->width * $this->height;
}
}
$rect = new Rectangle(3.0, 4.5);
echo "Area: ", $rect->area(), " cm2\n";
The Shape class defines area() and nothing else. Rectangle stores the two dimensions and implements the method with its own formula. The return type float is part of the contract, so every shape returns a number in the same form and calling code does not need to know which subclass it holds.
$ php abs_basic.php Area: 13.5 cm2
Concrete methods in an abstract class
An abstract class can mix abstract methods with finished ones. The finished method can call the abstract methods, which lets the parent own the common logic.
<?php
declare(strict_types=1);
abstract class Report
{
abstract protected function title(): string;
abstract protected function rows(): array;
public function render(): void
{
echo "== ", $this->title(), " ==\n";
foreach ($this->rows() as $row) {
echo "- ", $row, "\n";
}
}
}
class SalesReport extends Report
{
protected function title(): string
{
return "Sales";
}
protected function rows(): array
{
return ["North: 120", "South: 90"];
}
}
$report = new SalesReport();
$report->render();
render() is written once and never needs to know which report it is printing. It asks the subclass for the title and the rows through the abstract methods, then formats them. SalesReport supplies only the two pieces of data. The abstract methods are protected, because they are an internal contract rather than part of the public API.
$ php abs_concrete.php == Sales == - North: 120 - South: 90
The template method pattern
When the finished method defines the order of the steps, it is called a template method. Marking it final stops a subclass from replacing the algorithm while still allowing the steps to vary.
<?php
declare(strict_types=1);
abstract class Importer
{
final public function run(string $source): void
{
$rows = $this->parse($source);
foreach ($rows as $row) {
$this->store($row);
}
echo "Imported ", count($rows), " rows\n";
}
abstract protected function parse(string $source): array;
abstract protected function store(string $row): void;
}
class CsvImporter extends Importer
{
protected function parse(string $source): array
{
return array_map("trim", explode("\n", trim($source)));
}
protected function store(string $row): void
{
echo "Stored: ", $row, "\n";
}
}
$importer = new CsvImporter();
$importer->run("pen\nnotebook\neraser");
Importer::run() fixes the sequence: parse the source, store every row, report the count. CsvImporter only fills in parse() and store(). Because run() is final, no subclass can skip the counting or reorder the steps. This is the template method pattern, and the abstract methods are its hooks. The PHP final keyword article covers the modifier itself.
$ php abs_template.php Stored: pen Stored: notebook Stored: eraser Imported 3 rows
An abstract hierarchy
An abstract class may extend another abstract class. Each level can add requirements, and only a concrete class at the end of the chain implements all of them.
<?php
declare(strict_types=1);
abstract class Model
{
abstract public function table(): string;
}
abstract class SoftDeletes extends Model
{
abstract public function deletedAt(): ?string;
}
class Post extends SoftDeletes
{
public function table(): string
{
return "posts";
}
public function deletedAt(): ?string
{
return "2026-11-20 10:00:00";
}
}
$post = new Post();
echo $post->table(), "\n";
echo "Deleted at: ", $post->deletedAt(), "\n";
Model requires a table name, and SoftDeletes adds the deletion timestamp without implementing the first method. Post is the first concrete class and answers both. This kind of chain is useful when a framework offers a base model and an optional behaviour layer, because a class that does not need soft deletes can extend Model directly.
$ php abs_hierarchy.php posts Deleted at: 2026-11-20 10:00:00
A constructor in an abstract class
Abstract classes can declare constructors and properties like any other class. The constructor usually stores the state that the abstract methods work with.
<?php
declare(strict_types=1);
abstract class Employee
{
public function __construct(
protected string $name,
protected int $id
) {
}
abstract public function monthlyPay(): int;
public function describe(): string
{
return sprintf("%s (#%d) earns %d", $this->name, $this->id, $this->monthlyPay());
}
}
class Contractor extends Employee
{
public function __construct(
string $name,
int $id,
private int $rate,
private int $hours
) {
parent::__construct($name, $id);
}
public function monthlyPay(): int
{
return $this->rate * $this->hours;
}
}
echo (new Contractor("Alice", 7, 40, 120))->describe(), "\n";
Employee owns the name and the identifier and implements describe(), which calls the abstract monthlyPay(). Contractor passes the shared values to parent::__construct() and adds the rate and the hours. The parent never knows how pay is calculated, yet it can produce a complete description. The promoted constructor parameters are explained in constructor property promotions.
$ php abs_constructor.php Alice (#7) earns 4800
Instantiating an abstract class fails
The restriction is enforced at run time by the engine. The attempt raises an Error that names the class.
<?php
declare(strict_types=1);
abstract class Shape
{
abstract public function area(): float;
}
try {
$shape = new Shape();
} catch (Error $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
The message Cannot instantiate abstract class Shape is an Error, the same base class used for type errors, so a test can catch it. In application code the exception is not something to handle: it means the code asked for an incomplete class instead of one of its concrete subclasses.
$ php abs_instantiate.php Error: Cannot instantiate abstract class Shape
A missing implementation is a compile error
A concrete subclass that forgets an abstract method is rejected before the script runs.
<?php
declare(strict_types=1);
abstract class Animal
{
abstract public function speak(): string;
}
class Dog extends Animal
{
public function bark(): string
{
return "Woof";
}
}
PHP reports Class Dog contains 1 abstract method and must therefore be declared abstract or implement the remaining method (Animal::speak). The message offers both ways out: implement the method, or mark Dog abstract and let a further subclass finish the job. This check is what makes the abstract signature a real contract rather than a comment.
$ php abs_incomplete.php
Fatal error: Class Dog contains 1 abstract method and must therefore be declared abstract or implement the remaining method (Animal::speak) in /app/abs_incomplete.php on line 10
Stack trace:
#0 {main}
Abstract methods cannot be private
Visibility matters in the contract. A private abstract method cannot be implemented by a child, so PHP rejects the declaration.
<?php
declare(strict_types=1);
abstract class Task
{
abstract private function run(): void;
}
The declaration produces Abstract function Task::run() cannot be declared private. protected and public are both valid for an abstract method. A child may implement a protected abstract method as public, because that widens access, but it cannot reduce a public abstract method to protected.
$ php abs_private.php
Fatal error: Abstract function Task::run() cannot be declared private in /app/abs_private.php on line 7
Stack trace:
#0 {main}
Best practices
Use an abstract class when subclasses should share state or code, and an interface when only a contract is needed.
- Keep abstract methods few and focused; a long list is a sign that the class is doing too much.
- Put shared state and finished methods in the abstract class, and leave only the varying steps abstract.
- Prefer protected abstract methods for internal hooks and public ones for the API the caller uses.
- Make template methods final when subclasses must not change the order of the steps.
- Do not declare a class abstract as a substitute for a factory; document how callers obtain a concrete instance.
The PHP OOP tutorial explains the surrounding class model, and PHP interfaces shows the contract-only alternative.
Source
PHP manual: abstract classes and methods
This article covered the PHP abstract keyword: abstract classes and methods, concrete methods in a base class, the template method pattern, abstract hierarchies, and the checks the engine performs. Continue with PHP interfaces for pure contracts, or read PHP final keyword for the modifier used on the template method.
Author
List all PHP tutorials.