class Animal {
public function __construct(protected string $name) {}
public function speak(): string { return '...'; }
}
class Dog extends Animal {
public function speak(): string { return 'Woof!'; }
public function fetch(): void { /* ... */ }
}
$d = new Dog('Rex');
$d->speak(); // "Woof!" — overriding
$d->fetch(); // own method
Inheritance is often overused. Ask: "is B genuinely a kind of A?" (is-a). If yes — fine. If you are inheriting only to reuse code — prefer Composition or a Trait.
Embedding objects instead of inheriting is more flexible. Instead of class EmailService extends Mailer → class EmailService { private Mailer $mailer; }.