EmailValueObject.php

November 1, 2014 ยท View on GitHub

mailbox, $this->host) = explode('@', $email); } public function __toString() { return sprintf('%s@%s', $this->mailbox, $this->host); } public function __set($field, $value) { } public function changeMailbox($newMailbox) { $copy = clone $this; $copy->mailbox = $newMailbox; return $copy; } } // value object works as expected $email = new EmailValueObject('john@example.com'); assert((string) $email == 'john@example.com'); // new instance of value object is created as well $changed = $email->changemailbox('wojtek'); assert((string) $changed == 'wojtek@example.com'); // old is not affected, hasn't been mutated assert((string) $email == 'john@example.com');