Last active
November 5, 2018 13:34
-
-
Save berdyshev/5884995 to your computer and use it in GitHub Desktop.
Class which handles user's account using PDO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Account | |
*/ | |
class Account { | |
private $uid; | |
private $name; | |
private $surname; | |
private $email; | |
private $password; | |
/** | |
* Fill the object fields with new values. | |
* | |
* @param string $name User name. | |
* @param string $surname User surname. | |
* @param string $email Email | |
* @param string $password Password. Will be encoded with md5(). | |
*/ | |
public function create($name, $surname, $email, $password) { | |
$this->uid = NULL; | |
$this->name = $name; | |
$this->surname = $surname; | |
$this->email = $email; | |
$this->password = md5($password); | |
} | |
/** | |
* Setter for the class fields. | |
* | |
* @param string $name Field name. | |
* @param mixed $value Field value. | |
*/ | |
public function __set($name, $value) { | |
if (isset($this->$name)) { | |
if ($name == 'password') { | |
$value = md5($value); | |
} | |
$this->$name = $value; | |
} | |
} | |
/** | |
* Getter for the class fields. | |
* | |
* @param string $name Field name to return. | |
* @return mixed | |
* Value of the specified field if there is such field, otherwise - NULL. | |
*/ | |
public function __get($name) { | |
if (isset($this->$name)) { | |
return $this->name; | |
} | |
return NULL; | |
} | |
/** | |
* Saves the object values into DB. | |
* If there is UID value, the record will be updated, otherwise - inserted. | |
*/ | |
public function save() { | |
$db = new PDO('mysql:host=localhost;dbname=sites_sandbox;charset=utf8', 'root', '123Qwe'); | |
$params = array( | |
':name' => $this->name, | |
':surname' => $this->surname, | |
':email' => $this->email, | |
':password' => $this->password, | |
); | |
if (empty($this->uid)) { | |
$query = 'INSERT INTO accounts (name, surname, email, password) VALUES (:name, :surname, :email, :password)'; | |
} | |
else { | |
$query = 'UPDATE accounts SET name=:name, surname=:surname, email=:email, password=:password WHERE uid=:uid'; | |
$params[':uid'] = $this->uid; | |
} | |
$db->prepare($query)->execute($params); | |
} | |
/** | |
* Loads the account from the DB by user's id. | |
* @param int $uid User ID. | |
* @return mixed | |
* Returns Account object. | |
*/ | |
public static function load($uid) { | |
$db = new PDO('mysql:host=localhost;dbname=sites_sandbox;charset=utf8', 'root', '123Qwe'); | |
$q = $db->prepare('SELECT * FROM accounts WHERE uid = :uid'); | |
$q->setFetchMode(PDO::FETCH_CLASS, 'Account'); | |
$q->execute(array(':uid' => $uid)); | |
$account = $q->fetch(); | |
return $account; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo on line 53 - should be $name, not name
Other than that - thanks!