Most of the time, the Identity of an Entity is represented as a primitive type — usually a string or an integer. But using a Value Object to represent it has more advantages:
- Value Objects are immutable, so they can't be modified.
- Value Objects are complex types that can have custom behaviors, something which primitive types can't have. Take, as an example, the equality operation. With Value Objects, equality operations can be modeled and encapsulated in their own classes, making concepts go from implicit to explicit.
Let's see a possible implementation for OrderId
, the Order
Identity that has evolved into a Value Object:
namespace Ddd\Billing\Domain\Model; class OrderId { private $id; public function __construct($anId) { $this->id = $anId; } public function id() { return $this->id; } public function equalsTo(OrderId $anOrderId) { return $anOrderId->id === $this->id; } }
There are different...