Repository navigation
Rule 4 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.
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.
- The host checker is a list of small checks (
src/Doctor/PhpCheck.phpandsrc/Doctor/HostCheck.php), combined bysrc/Doctor/Doctor.php. Nothing extends anything. -
hammeris a list of commands (src/Console/Command.php), combined bysrc/Console/Application.php. - Signing in is several small pieces (
src/Auth/PasswordHasher.php,src/Auth/Totp.php,src/Auth/SignInThrottle.php) used together by one action.
Extending a framework's base class is sometimes required (an exception extends
RuntimeException). That is an "is-a" relationship, and it is fine.
Interface, binding. See Glossary.
PHP 8.5 OOP Mastery, Rule 4 in its course philosophy, taught in lesson 1.4, composition over inheritance. Course page.