php-value-objects
php-value-objects copied to clipboard
A resources site for using Value Objects in PHP
Welcome to PHP Value Objects
Value Objects are an essential concept when implementing Domain Driven Design, but they can be used in any PHP project. @cakper's recent blog post is a good introduction, and we'll link to more resources in the coming weeks.
Value Object Basics
A Value Object is a simple object that is defined by its properties, and reflects a concept from the business domain - e.g. Money, Email Address, Temperature, Location etc. The main characteristic of a Value Object is that it is immutable. This means that it has
- all properties initialised in the constructor
- no setters
If you want to modify a Value Object, you must create a new one.
You can use a Value Object anywhere you might have used a simple scalar value or an Associative Array. The benefits are many, including:
- it can be a type-hinted parameter in a method
- you won't miss-spell the key when getting values
- you can have Null Value Objects
- it can contain business/domain logic
Using a Value Object will help you encapsulate behaviour more easily. This will make your code easier to understand and reduce the chance of errors.
If you are working with legacy code, refactoring to extract Value Objects is a relatively simple process that will pay dividends.
Learn more about Value Objects
- Wikipedia
- The Elephant in the Room Podcast, Episode 2 by @mathiasverraes and @everzet (2013-10)
- Value Objects and User Interfaces by @mathiasverraes (2013-11)
- Persisting Value Objects in Doctrine by @rosstuck (2014-01)
- Client validations VS protecting domain consistency by @Delendial (2014-06)
- DDD Building Blocks in PHP: Value Objects by @cakper (2014-10)
- Validating Value Objects by @cakper (2014-10)
- Value Objects by @mr_r_miller (2014-11)
- Value Objects and Immutability by @mr_r_miller (2014-11)
- Domain Driven Design in PHP
Implementations
Money
- https://github.com/mathiasverraes/money by @mathiasverraes
- https://github.com/sebastianbergmann/money by @s_bergmann
Libraries
- https://github.com/gws/php-valueobjects by @gws
- https://github.com/nicolopignatelli/valueobjects by @nicolopigna
Who to follow on Twitter
- Mathias Verraes @mathiasverraes
- Konstantin Kudryashov @everzet
- PHPValueObjects @phpvalueobjects
- Kacper Gunia @cakper
- Benjamin Dulau @Delendial
- Ross Tuck @rosstuck
- Richard Miller @mr_r_miller
If you want to suggest a resource, email suggestions (at) phpvalueobjects (dot) info.