class UserMapper {
protected $db;
public function __construct(PDO $db) {
$this->db = $db;
}
public function save(User $user_object)
{
}
public function getUserById($id)
{
//code
}
}
class User {
private $id;
private $username;
function __construct($user_row = null)
{
if (!is_null($user_row)) {
$this->id = $user_row->id;
$this->username = $user_row->username;
}
}
public function __set($name, $value)
{
}
public function __get($name)
{
return $this->$name;
}
}
例:
$user = new User();
$user->username = 'New Username';
$user_mapper = new UserMapper($db); //Pdo object = $db
$user_mapper->save($user);
これはパターンを強制する正しい方法ですか?