Glossary

Inheritance

Inheritance is a mechanism where a child class receives the properties and methods of a parent class and can override or extend them. PHP supports single inheritance: a class can have only one parent.

Basic example

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

When NOT to use it

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.

Composition over Inheritance

Embedding objects instead of inheriting is more flexible. Instead of class EmailService extends Mailerclass EmailService { private Mailer $mailer; }.