PHP Inheritance
Inheritance lets one class reuse and extend another. The new class (the child or subclass) automatically gets all public and protected properties and methods of the existing class (the parent or superclass), and can add its own or change the inherited ones.
In PHP you inherit with the extends keyword. A class can extend only one parent (single inheritance); traits and interfaces, covered later, fill the gaps.
Extending a class
<?php
class Animal {
public function __construct(protected string $name) {}
public function eat(): string {
return "{$this->name} is eating";
}
public function speak(): string {
return "{$this->name} makes a sound";
}
}
class Dog extends Animal {
public function fetch(): string {
return "{$this->name} fetches the ball";
}
}
$rex = new Dog('Rex');
echo $rex->eat() . "\n"; // inherited
echo $rex->speak() . "\n"; // inherited
echo $rex->fetch() . "\n"; // new in Dog
var_dump($rex instanceof Animal);Rex is eating
Rex makes a sound
Rex fetches the ball
bool(true)
Dog did not declare a constructor, so it uses the one from Animal. A Dog is an Animal, which is why instanceof Animal is true.
Overriding methods
A child can replace an inherited method by declaring one with the same name. Inside the override, parent::method() calls the parent's version:
<?php
class Animal {
public function __construct(protected string $name) {}
public function speak(): string {
return "{$this->name} makes a sound";
}
}
class Cat extends Animal {
public function speak(): string {
return "{$this->name} says meow";
}
}
class LoudCat extends Cat {
public function speak(): string {
return strtoupper(parent::speak()) . '!';
}
}
foreach ([new Animal('Generic'), new Cat('Tom'), new LoudCat('Max')] as $a) {
echo $a->speak() . "\n";
}Generic makes a sound
Tom says meow
MAX SAYS MEOW!
The loop calls speak() on each object and gets the most specific version — this is polymorphism in action.
Child constructors
If the child defines its own __construct, the parent's constructor is not called automatically. Call it with parent::__construct(...):
<?php
class Employee {
public function __construct(
protected string $name,
protected float $salary,
) {}
public function summary(): string {
return sprintf('%s earns %.0f', $this->name, $this->salary);
}
}
class Manager extends Employee {
public function __construct(string $name, float $salary, private int $teamSize) {
parent::__construct($name, $salary);
}
public function summary(): string {
return parent::summary() . " and leads {$this->teamSize} people";
}
}
echo (new Employee('Hoa', 3000))->summary() . "\n";
echo (new Manager('Duc', 5200, 6))->summary() . "\n";Hoa earns 3000
Duc earns 5200 and leads 6 people
Override rules
PHP checks that an override stays compatible with the parent (the Liskov substitution principle):
| Rule | Example |
|---|---|
| Visibility may stay the same or become wider | protected → public is fine; public → private is an error |
| Parameter types may become wider | parent int $x, child `int |
| Return type may become narrower | parent : ?string, child : string |
| Required parameters cannot be added | extra parameters need default values |
The #[\Override] attribute (PHP 8.3) tells PHP "this method is meant to override something". If the parent method is renamed or removed, you get an error instead of a silent bug.
Stopping inheritance with final
Mark a method final to forbid overriding it, or a whole class final to forbid extending it:
<?php
class Invoice {
final public function number(): string {
return 'INV-' . $this->id();
}
protected function id(): int { return 7; }
}
final class PaidInvoice extends Invoice {
protected function id(): int { return 42; } // allowed: id() is not final
}
echo (new Invoice())->number() . "\n";
echo (new PaidInvoice())->number() . "\n";
var_dump((new ReflectionClass(PaidInvoice::class))->isFinal());INV-7
INV-42
bool(true)
Trying to write class Special extends PaidInvoice {} or to override number() would be a fatal compile error, so it cannot be shown in a runnable example.
Tip: Prefer short inheritance chains. When a class only needs to use another class, giving it that object as a property (composition) is usually simpler than extending it.
Test yourself
Which keyword makes a class inherit from another class?
implementsinheritsextendsuse
A child class defines its own __construct. Does the parent constructor run?
- Yes, always before the child's
- Yes, always after the child's
- Only if the child calls
parent::__construct() - Never, it is not allowed
What does final on a method do?
- Makes it static
- Prevents child classes from overriding it
- Makes it private
- Runs it last