Implement Value Objects for Domain Modeling

Implement Value Objects for Domain Modeling

Use Value Objects to represent domain concepts that are defined by their attributes rather than an identity. This enhances code readability and ensures data integrity.

final class Email
{
    private $email;

    public function __construct(string $email)
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException('Invalid email');
        }
        $this->email = $email;
    }

    public function __toString(): string
    {
        return $this->email;
    }
}
← Back to Tips List