A php trait that helps safe update for immutable objects.
Immutable objects and complete constructor are useful idea.
Sometimes, you wants to replace partial properties of object. You should instantiate new object with new value, because immutable object is not changeable. Or you may define a method in this case, like this;
public function withRequest($request): self
{
$new = clone $this;
$new->request = $request;
return $new;
}
But there are some problems;
- Boilerplate code.
- Validation is troublesome.
This library is intended to solve this problem.
$ composer require atiara/immutable-update
Please see example.
MIT.