/**
  * Magic method for setting properties.
  */
 public function __set($name, $value)
 {
     switch ($name) {
         case 'name':
             $value = trim($value);
             if (strlen($value) < self::MIN_USERNAME_LENGTH) {
                 // Usage of strlen() intentional for unicode characters
                 throw new ErrorException('Username should be at least ' . self::MIN_USERNAME_LENGTH . ' characters.');
             }
             if (mb_strlen($value) > self::MAX_USERNAME_LENGTH) {
                 throw new ErrorException('Username may not exceed ' . self::MAX_USERNAME_LENGTH . ' characters.');
             }
             if (SessionUser::IllegalUsername($value)) {
                 throw new ErrorException('Username is reserved. Please choose another username.');
             }
             try {
                 $class = get_called_class();
                 $user = $class::FindByName($value);
                 throw new ErrorException('Username already exists. Please choose another username.');
             } catch (ActiveRecord_NotFoundException $e) {
                 // "Not Found" is the desired result
             }
             parent::__set($name, $value);
             break;
         case 'password':
             throw new ErrorException('Use <i>SetPassword()</i> to change password.');
         case 'email':
             if (!empty($value)) {
                 $value = filter_var($value, FILTER_VALIDATE_EMAIL);
                 if ($value == false) {
                     throw new ErrorException('Email address is not valid. Please check if you entered it correctly.');
                 }
                 // Check if email host actually exists (parse_url() is faked into thinking email address is URL to get hostname)
                 $parts = parse_url('http://' . $value . '/');
                 if (!isset($parts['host']) || filter_var($parts['host'], FILTER_VALIDATE_IP) == false && gethostbyname($parts['host']) == $parts['host']) {
                     throw new ErrorException('Your email host <i>' . SafeHTML($parts['host']) . '</i> does not appear to exist.');
                 }
                 parent::__set($name, $value);
             }
             break;
         default:
             parent::__set($name, $value);
     }
 }