I've been working some forth looking on design patterns, and I would like some help to improve the code (fixes, tips, just generally improvements, or things you consider bad practise, etcetera in my code).
And finally: Am I on the right track, or should just back the wheel and start over?
UserMapper
<?php
class UserMapper {
protected $db;
function __construct($db)
{
$this->db = $db;
}
public function save(User $user_object)
{
$data = array(
'username' => $user_object->username,
'email' => $user_object->email,
'password' => $user_object->password,
);
if (is_null($user_object->id)) {
$data['salt'] = $user_object->salt;
$data['created'] = time();
$sth = $this->db->prepare("INSERT INTO users (username, email, password, salt, created) VALUES (:username, :email, :password, :salt, :created)");
$sth->execute($data);
} else {
$data['id'] = $user_object->id;
$sth = $this->db->prepare("UPDATE `users` SET username = :username, email = :email, password = :password WHERE `id` = :id");
$sth->execute($data);
}
}
public function getUserById($id)
{
$sth = $this->db->prepare("SELECT * FROM users WHERE id = ?");
$sth->execute(array($id));
$sth->setFetchMode(PDO::FETCH_OBJ);
if ($sth->rowCount() == 0) {
throw new Exception('User not found');
}
$row = $sth->fetch();
$user_object = new User($row);
return $user_object;
}
}
?>
User:
<?php
class User {
private $id;
private $username;
private $email;
private $password;
private $salt;
private $created;
public function __construct($user_row = null)
{
if (!is_null($user_row)) {
$this->id = $user_row->id;
$this->username = $user_row->username;
$this->email = $user_row->email;
$this->password = $user_row->password;
$this->salt = $user_row->salt;
$this->created = $user_row->created;
}
}
public function __set($name, $value)
{
switch ($name) {
case 'password':
$value = sha1($value . $this->salt);
break;
}
$this->$name = $value;
}
public function __get($name)
{
return $this->$name;
}
}
Auth...
A empty class with logged_in, login, logout, methods.
function save(User $user) { $data = array();
$data['username'] = $user->username;
$data['password'] = $user->password;
if (is_null($user->id)) {
$data['salt'] = $user->salt;
$columns = implode(', ', array_keys($data));
$values = implode(', :', array_keys($data));
try {
$sth = $this->db->prepare("INSERT INTO `users` ($columns) VALUES (:$values)");
$sth->execute($data);
} catch (PDOException $e) {
echo $e->getMessage();
}
} else {
$data['id'] = $user->id;
$dataSet = '';
foreach (array_keys($data) as $key) {
$dataSet .= "$key = :$key, ";
}
$sth = $this->db->prepare("UPDATE `users` SET $dataSet WHERE `id` = :id");
$sth->execute($data);
}
}
test: (all files needed included)
$host = 'localhost';
$dbname = 'code';
$user = 'root';
$pass = '';
try {
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
$user = new User;
$user->username = 'A123123ro';
$user->password = 'h1231233';
$user->salt = 'hej';
$user->email = 'j213123e';
$user_mapper = new UserMapper($db);
$user_mapper->save($user);