A value object is just any immutable object that wraps primitive-type values. Entities, value objects and repositories are the materials which services use to perform their tasks.

Below are some of the characteristics of a value object
It adds information about the data (e.g. This is not just a number it is an Age of a person)
It validates the data (e.g. this isn’t just any int, it’s a Adult age from 18 to 110 years).
If it wraps primitive-type data like string, int, float.
If it’s immutable.
It helps repeating validating object.

Value object example

public class EmployeeAge
	private int $age;

	private function __construct(int $age)
		Assertion::between($age, 18, 110);
		$this->age = $age;

	public function inTenYears(): EmployeeAge
		return new EmployeeAge($this->age + 10);

I think most of the points of a value object are really simple to understand apart from being immutable.

What is immutable object?
Lets see from the example of Value object class EmployeeAge

$age = new EmployeeAge(25);

The method inTenYears() does not change the data "25" that $age contains. Instead, a new EmployeeAge object is instantiated and given the data "35" during its construction. A reference to this EmployeeAge object is returned by the inTenYears() method. To make the EmployeeAge $age contain the data "35", a different approach is needed:

$age = $age->inTenYears();

Now the class EmployeeAge $age references a new EmployeeAge object that contains "35".

By returning a new copy instead of manipulating the existing object, we effectively make the EmployeeAge value object immutable.

What if value object needs to extra some information from external resource?

If a value object needs a service to perform some task, you could inject it as a method argument,

what clients say about us

Client testimonials

Our sites look great and run smoothly. Thanks for all your hard work and patience in getting us to where we are today.

contact us

Contact Us

If you have questions or want us to send you a quote for project please email us.

latest news


PHPUnit mock magic functions
Learn more about PHPUnit Partial Mock.