Skip to main content

Value Objects

A value object wraps a scalar (string, int, float) and guarantees it is valid. If you hold an Email, it is a valid email address — no further checks needed.

function sendNewsletter(Email $email, Name $name): void {
// $email and $name are guaranteed to be valid here
}

The package n2n/valobj provides:

  • Ready-to-use types like Email, Phone, Name, Text, NbId or PasswordHash.
  • Base classes like CleanString, NonSpecialCharsString or PositiveInt to build your own types with a few lines.
  • n2n Bind support — every type can be unmarshalled from request data and marshalled back.
  • ORM support — value objects can be used directly as property types in entities.

Topics​