Primitive Obsession is a common PHP anti-pattern where strings, floats, and ints are used instead of meaningful domain types. Value Objects solve this by encapsulating validation, immutability, and behavior into small, focused classes. Using PHP 8.2+ readonly classes, examples show how to model Price (with currency and integer cents) and EmailAddress (with built-in validation) as Value Objects. This approach improves type safety, reduces duplication, and makes code self-documenting. The post also covers when Value Objects are justified, how to add domain behavior beyond simple data holding, and addresses performance concerns in modern PHP.
Table of contents
The “Price” of Primitive ObsessionWhat Exactly is a Value Object?A Better Approach: Explicit Domain TypesWhy This Matters (Especially Today)Real-World Refactoring: EmailLogic-Heavy Value ObjectsWhen NOT to Use Value ObjectsPerformance ConsiderationsSummary3 Comments
Sort: