Inheritance
class Cat extends Animal gives Cat every public and protected property and method Animal has, automatically. Cat can override any inherited method simply by redeclaring it with the same name — and from inside that override, parent::methodName() calls the original, unoverridden version from the parent class, letting a subclass extend behavior instead of fully replacing it.
<?php
class Animal {
protected string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function speak(): string {
return "$this->name makes a sound.";
}
}
class Cat extends Animal {
public function speak(): string {
return parent::speak() . " Actually, $this->name says Meow.";
}
}
$c = new Cat("Whiskers");
echo $c->speak(), "\n";
Whiskers makes a sound. Actually, Whiskers says Meow.
$c->speak() ran Cat's override, which itself calls parent::speak() to get Animal's original sentence before appending its own — both sentences show up in the single final string. $name is declared protected rather than private specifically so Cat (and any other subclass) can read $this->name directly; had it been private, only Animal's own methods could touch it at all. PHP supports only single inheritance — one extends per class — which is exactly why interfaces and traits, next, exist as its two different answers to "I need more than one parent."
Practice exercise
Write a Vehicle class with a protected $name and a describe() method returning "$name is a vehicle.", then a Car extends Vehicle that overrides describe() to return parent::describe() plus " Specifically, a car." — echo the result for a Car named "Sedan".