Skip to content

Rule 5 State or work rarely both

Edward Baitsewe edited this page Nov 9, 2026 · 1 revision

Rule 5: Objects either hold state or perform work, rarely both

Part one: wiring. An object either holds data and protects it, or does a job and remembers nothing. Not both.

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

An object that holds data and also does work is hard to test (you must set up its data to test its work) and hard to trust (its work can change its data in the middle of a request). Keeping the two apart makes each one simple.

Where it shows in Slim Killer

When not to apply it

Some objects really are both, like a session or a stream. Keep them few and say so in the class comment.

Words

Value object, service. See Glossary.

Learn more

PHP 8.5 OOP Mastery, Rule 5 in its course philosophy, taught in module 6, object lifecycle and state. Course page.


Build philosophy

Clone this wiki locally