php-value-objects icon indicating copy to clipboard operation
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

Implementations

Money

Libraries

Who to follow on Twitter

If you want to suggest a resource, email suggestions (at) phpvalueobjects (dot) info.