Skip to content

Chapter 19 of 38

Inheritance

Extend behavior safely with overrides, final contracts, parent calls, and composition.

36 minutes 10 quick checksBy Subha Prasad
Lesson 19 of 38Course navigation

Lesson content

Read, practise, then check your understanding

A PHP class extends one parent. Overrides must honor signature and semantic expectations. Final can close a class or method when extension would break invariants.

Practical example

<?php
abstract class Report { abstract public function render(): string; }
final class HtmlReport extends Report {
  public function render(): string { return '<article>Safe content</article>'; }
}

Use parent:: only where extension is designed. Protected mutable fields tightly couple descendants; prefer private state. Choose composition for has-a relationships and reusable policy. Inheritance should express real substitutability, not code-copy avoidance.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes extends?
Which PHP term matches this description: The keyword establishing single class inheritance.
Which statement best describes override?
Which PHP term matches this description: A child method replacing inherited behavior with a compatible contract.
Which statement best describes final?
Which PHP term matches this description: A modifier preventing class extension or method override.
Which statement best describes parent::?
Which PHP term matches this description: Syntax accessing a parent class member implementation.
Which statement best describes composition?
Which PHP term matches this description: Combining collaborator objects instead of inheriting for reuse.

0 of 10 checks passed

Your progress is saved on this device.

Inheritance | PHP Lesson | Subha Prasad