ik ben terug aan het programmeren geslagen en wou nog eens proberen een OOP member systeem maken. Ik heb daarvoor het dataMapper patern gebruikt dat WouterJ me eens had geleerd.
Kunnen jullie eens kijken of mijn structuur goed zit?

User_User
<?php
/* user class */
class User_User
{
private $name;
private $password;
private $email;
private $id;
public function __construct($name, $password, $email)
{
$this->name = (string) $name;
$this->password = (string) $password;
$this->email = (string) $email;
}
public function setId($value)
{
$this->id = $value;
}
public function setName($value)
{
$this->name = $value;
}
public function setPassword($value)
{
$this->password = $value;
}
public function setEmail($value)
{
$this->email = $value;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getPassword()
{
return $this->password;
}
public function getEmail()
{
return $this->email;
}
}
?>
User_UserMapper
<?php
class User_UserMapper
{
protected $db; // db abstractielaag
public function __construct(PDO $db)
{
$this->db = $db;
}
/**
* Uit de database komt een array, populate maakt van de array een User object
*/
public function populate(array $data)
{
$user = new User_User($data['name'], $data['password'], $data['email']);
$user->setId($data['id']);
return $user;
}
public function getUserById($id)
{
$qry = $this->db->prepare("SELECT name, password, email FROM users WHERE id = ?");
$qry->execute(array((int) $id));
$result = $qry->fetch(PDO::FETCH_ASSOC);
$result['id'] = (int) $id;
return $this->populate($result);
}
public function getUserByPassAndEmail($email, $password)
{
$qry = $this->db->prepare("SELECT id, name FROM users WHERE email = ? AND password = ?");
$qry->execute(array($email, $password));
$result = $qry->fetch(PDO::FETCH_ASSOC);
$result['email'] = $email;
$result['password'] = $password;
return $this->populate($result);
}
public function saveUser(User $user)
{
}
public function deleteUserById($id)
{
}
}
?>
Session_Session
<?php
class Session_Session
{
public function __construct($id)
{
$_SESSION['user_id'] = $id;
}
}
?>
Public/index.php
<?php
include('../lib/autoLoader.php');
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$userMapper = new User_UserMapper($db);
$jasper = $userMapper->getUserByPassAndEmail('jasp.***@gmail.com', 'test123');
echo $jasper->getName(); // geeft jasper -> OK
new Session_Session($jasper->getId());
echo $_SESSION['user_id']; // geeft 1 -> OK
?>
Bedankt!