Skip to content

Rule 4 Composition over inheritance

Edward Baitsewe edited this page Nov 9, 2026 · 2 revisions

Rule 4: Favour composition over inheritance

Part one: wiring. Build behaviour by combining small objects, not by extending classes.

The short form of every rule is in BUILD_PHILOSOPHY.md, which is the version that counts. This page is the long form: why, where it shows today, and when not to use it.

Why

Inheritance ties a child to every detail of its parent, including details nobody meant to share. Composition ties an object only to the interface it uses. For Slim Killer this is also the upgrade promise: a client site changes behaviour by binding its own implementation, never by extending a core class that may change in the next release.

Where it shows in Slim Killer

When not to apply it

Extending a framework's base class is sometimes required (an exception extends RuntimeException). That is an "is-a" relationship, and it is fine.

Words

Interface, binding. See Glossary.

Learn more

PHP 8.5 OOP Mastery, Rule 4 in its course philosophy, taught in lesson 1.4, composition over inheritance. Course page.


Build philosophy

Clone this wiki locally