What it is and why to use it
An abstract class is a class that cannot be instantiated directly. It can contain ordinary methods and state as well as abstract methods: method signatures that concrete subclasses must implement. Use one when several related objects share a meaningful implementation, while a particular operation differs between them. An interface instead describes a contract without requiring a shared parent implementation.
How it works
Declare the class with abstract class and an unfinished method with abstract function followed by a semicolon, not a body. A concrete subclass extends it and supplies compatible method signatures. A protected abstract method can be implemented as public, but an inherited public contract cannot be narrowed to protected. The examples use methods; abstract properties and property hooks belong to newer PHP 8.4 features and are outside this introduction.
Common mistakes and limitations
Do not instantiate the abstract parent or omit one of its required methods from a concrete child. Keep the parameter and return contracts compatible. Avoid creating inheritance hierarchies merely to reuse a few unrelated lines; a helper or composed collaborator may express the relationship more clearly.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
abstract class Report {
abstract public function title(): string;
}Examples
Supply the varying part of a shared report
Keep the formatting in one place and let the child provide its title.
<?php
declare(strict_types=1);
abstract class Report {
abstract public function title(): string;
public function heading(): string {
return 'Report: ' . $this->title();
}
}
class ProgressReport extends Report {
public function title(): string {
return 'Learning progress';
}
}
echo (new ProgressReport())->heading(), PHP_EOL;
- Report defines heading once but cannot supply title, so title is abstract.
- ProgressReport implements the required public method with the same string return contract.
- Calling heading uses the child’s title through $this. This pattern supports related reports that share formatting without duplicating it.
- Expected output: Report: Learning progress
Implement a protected calculation
Let a base class use a required internal operation without exposing that operation publicly.
<?php
declare(strict_types=1);
abstract class ScoreSummary {
abstract protected function points(): int;
public function line(): string {
return 'Points: ' . $this->points();
}
}
class CompletedLessons extends ScoreSummary {
protected function points(): int {
return 30;
}
}
echo (new CompletedLessons())->line(), PHP_EOL;
- The protected contract is intended for use inside the class hierarchy.
- CompletedLessons provides its calculation; the inherited public line method calls it.
- External code uses line rather than reaching into the calculation. This keeps the public API separate from the implementation detail.
- Expected output: Points: 30
Best practices
- Use abstract parents for genuinely related types with shared behaviour.
- Keep required method signatures small and stable.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual