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,NbIdorPasswordHash. - Base classes like
CleanString,NonSpecialCharsStringorPositiveIntto 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
- Install — install the package.
- Usage — create, compare and convert value objects.
- Ready-to-use types — all types the package ships with.
- Custom value objects — build your own types.
- n2n Bind — unmarshal request data into value objects.
- Persistence / ORM — value objects as entity property types.