Skip to content

Instantly share code, notes, and snippets.

@Martyr2

Martyr2/user.php

Created Oct 1, 2017
Embed
What would you like to do?
Simple User class with supporting Address class. This serves as a basic starting point for developing a quick user class for projects and can be expanded on further.
<?php
/**
* Basic user class to hold some standard data and designed to be expanded and altered as necessary.
*/
class User {
private $userID = 0;
private $firstName = '';
private $lastName = '';
private $address = null;
private $primaryEmail = '';
private $secondaryEmail = '';
private $primaryPhone = '';
private $secondaryPhone = '';
public function __construct() {
$this->address = new Address();
}
// Accessors (getters)
public function getId() { return $this->userID; }
public function getFirstName() { return $this->firstName; }
public function getLastName() { return $this->lastName; }
public function getFullName() { return $this->firstName . ' ' . $this->lastName; }
public function getAddress() { return $this->address; }
public function getPrimaryEmail() { return $this->primaryEmail; }
public function getSecondaryEmail() { return $this->secondaryEmail; }
public function getPrimaryPhone() { return $this->primaryPhone; }
public function getSecondaryPhone() { return $this->secondaryPhone; }
// Mutators (setters)
public function setId($id = 0) {
if (is_numeric($id)) {
$intId = (int)$id;
// We accept zero to show dirtiness or invalid user. Change as required.
if ($intId >= 0) {
$this->userID = (int)$id;
} else {
throw new Exception('User ID must be greater than or equal to 0.');
}
} else {
throw new Exception('User ID must be numeric.');
}
}
public function setFirstName($firstName) {
if (is_string($firstName)) {
$this->firstName = trim(strip_tags($firstName));
}
}
public function setLastName($lastName) {
if (is_string($lastName)) {
$this->lastName = trim(strip_tags($lastName));
}
}
public function setAddress($address = null) {
if (is_object($address) && is_a($address, 'Address')) {
$this->address = $address;
} else {
throw new Exception('Invalid address specified.');
}
}
public function setPrimaryEmail($email) {
if (is_string($email) && $this->validateEmail(trim($email))) {
$this->primaryEmail = trim($email);
} else {
throw new Exception('Invalid user primary email address specified.');
}
}
public function setSecondaryEmail($email) {
if (is_string($email) && $this->validateEmail(trim($email))) {
$this->secondaryEmail = trim($email);
} else {
throw new Exception('Invalid user secondary email address specified.');
}
}
public function setPrimaryPhone($phoneNumber) {
if (is_string($phoneNumber)) {
$this->primaryPhone = trim(strip_tags($phoneNumber));
} else {
throw new Exception('Invalid user primary phone number specified.');
}
}
public function setSecondaryPhone($phoneNumber) {
if (is_string($phoneNumber)) {
$this->secondaryPhone = trim(strip_tags($phoneNumber));
} else {
throw new Exception('Invalid user secondary phone number specified.');
}
}
// Private helper functions
private function validateEmail($email) {
// preg_match needed for PHP prior to 5.3
return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email);
}
}
/**
* Simple structure class for holding basic user address information.
*/
class Address {
public $address1 = '';
public $address2 = '';
public $city = '';
public $postal = '';
public $stateProvince = '';
public $country = '';
private $isBusiness = false;
public function setIsBusiness($isBusiness = false) {
$this->isBusiness = ($isBusiness !== false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.