Repository navigation
Rule 5 State or 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.
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.
- Data:
src/Auth/User.phpandsrc/Doctor/CheckResult.phpare read-only values. - Work:
src/Auth/UserRepository.phpreads and writes users;src/Doctor/Doctor.phpruns checks. Neither keeps anything between calls. - The one deliberate exception:
src/Http/Session/Session.phpholds one visitor's session data and tracks its own changes, because that is what a session is.
Some objects really are both, like a session or a stream. Keep them few and say so in the class comment.
Value object, service. See Glossary.
PHP 8.5 OOP Mastery, Rule 5 in its course philosophy, taught in module 6, object lifecycle and state. Course page.