What it is and why to use it
Inheritance lets a child class reuse and specialise a parent class. A child can inherit public and protected members and override eligible methods. The relationship should make sense as “is a”: a featured lesson is still a lesson. If an object merely uses another object’s service, composition is often the clearer relationship.
How it works
A class can extend one parent class and implement multiple interfaces. An override uses the same method name with a compatible signature. parent::method() explicitly calls the parent implementation. If a child declares its own constructor, PHP does not automatically call the parent constructor; call parent::__construct(...) when the parent’s initialisation is required. Private parent members are not directly accessible in the child.
Common mistakes and limitations
Do not narrow the visibility of an inherited public method or change its signature incompatibly. Do not forget required parent initialisation. A child that cannot satisfy the parent’s expected behaviour usually signals an unsuitable inheritance model. final methods and final classes deliberately restrict extension.
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
class FeaturedLesson extends Lesson { /* compatible overrides */ }Examples
Extend a label without repeating its base logic
Override one method and reuse the parent implementation.
<?php
declare(strict_types=1);
class Lesson {
public function label(): string {
return 'PHP arrays';
}
}
class FeaturedLesson extends Lesson {
public function label(): string {
return 'Featured: ' . parent::label();
}
}
echo (new FeaturedLesson())->label(), PHP_EOL;
- FeaturedLesson inherits from Lesson and overrides its public label method.
- parent::label explicitly calls the base implementation instead of recursively calling the child override.
- The child adds a prefix while keeping the parent’s text. A caller can still ask either type for a string label.
- Expected output: Featured: PHP arrays
Initialise parent and child state
Add a duration to an object whose parent already manages its title.
<?php
declare(strict_types=1);
class Lesson {
public function __construct(protected string $title) {}
public function title(): string {
return $this->title;
}
}
class TimedLesson extends Lesson {
public function __construct(string $title, private int $minutes) {
parent::__construct($title);
}
public function summary(): string {
return $this->title() . ': ' . $this->minutes . ' minutes';
}
}
echo (new TimedLesson('Functions', 15))->summary(), PHP_EOL;
- Constructor promotion creates the typed title and minutes properties in their respective classes.
- The child constructor forwards title to the parent so the inherited state is initialised.
- summary combines the inherited title method with the child’s private duration. Each example is a separate file, so its Lesson declaration does not conflict with the previous example.
- Expected output: Functions: 15 minutes
Best practices
- Use inheritance for substitutable related types.
- Call the parent constructor explicitly when adding child initialisation.
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