Skip to content

Instantly share code, notes, and snippets.

@kirkbushell
Last active May 30, 2017 10:06
Show Gist options
  • Save kirkbushell/aa60d05a39dba9288e25 to your computer and use it in GitHub Desktop.
Save kirkbushell/aa60d05a39dba9288e25 to your computer and use it in GitHub Desktop.
Value object usage within Laravel Eloquent example
<?php
// In model: Ensures values are consistent
class User extends Eloquent {
public function setEmailAttribute(Email $email) {
$this->attributes['email'] = $email;
}
}
// Value object
class Email {
public $email;
public function __construct($email) {
Assert::email($email);
$this->email = $email;
}
public function __toString() {
return $this->email;
}
}
@MohamedBoualleg
Copy link

thx for the example. But since Email is a value object, the $email propriety needs to be private

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment