<?
class Student {
private $properties;
function __construct($name, $age, $email){
$age = (int)$age;
$this->properties = array('name'=>$name, 'age'=>$age, 'email'=>$email); }
function getInfo(){
return $this->properties;
}
}
class DataMapper {
protected $db;
function __construct($db){
$this->db = $db;
}
function insertIntoDB(Student &$student){
$query = "INSERT INTO tbl (name, age, email) VALUES (:name, :age, :email)";
$stmt = $this->db->prepare($query);
$info = $student->getInfo();
$stmt->bindParam(':name', $this->db->quote($info['name']));
$stmt->bindParam(':age', $this->db->quote($info['age']));
$stmt->bindParam(':email', $this->db->quote($info['email']));
$stmt->execute();
}
function selectFromDB(){
//***
}
}
$student = new Student($_POST['name'], $_POST['age'], $_POST['email']);
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$mapper = new DataMapper($db);
$mapper->insertIntoDB($student);
PD8KY2xhc3MgU3R1ZGVudCB7Cglwcml2YXRlICRwcm9wZXJ0aWVzOwoJZnVuY3Rpb24gX19jb25zdHJ1Y3QoJG5hbWUsICRhZ2UsICRlbWFpbCl7CgkJJG5hbWUgPSBzdHJpcF90YWdzKHRyaW0oJG5hbWUpKTsKCQkkZW1haWwgPSBzdHJpcF90YWdzKHRyaW0oJGVtYWlsKSk7CgkJJGFnZSA9IChpbnQpJGFnZTsKCQkkdGhpcy0+cHJvcGVydGllcyA9IGFycmF5KCduYW1lJz0+JG5hbWUsICdhZ2UnPT4kYWdlLCAnZW1haWwnPT4kZW1haWwpOwoJfQoJZnVuY3Rpb24gZ2V0SW5mbygpewoJCXJldHVybiAkdGhpcy0+cHJvcGVydGllczsKCX0KfQpjbGFzcyBEYXRhTWFwcGVyIHsKCXByb3RlY3RlZCAkZGI7CglmdW5jdGlvbiBfX2NvbnN0cnVjdCgkZGIpewoJCSR0aGlzLT5kYiA9ICRkYjsKCX0KCWZ1bmN0aW9uIGluc2VydEludG9EQihTdHVkZW50ICYkc3R1ZGVudCl7CgkJJHF1ZXJ5ID0gIklOU0VSVCBJTlRPIHRibCAobmFtZSwgYWdlLCBlbWFpbCkgVkFMVUVTICg6bmFtZSwgOmFnZSwgOmVtYWlsKSI7CgkJJHN0bXQgPSAkdGhpcy0+ZGItPnByZXBhcmUoJHF1ZXJ5KTsKCQkkaW5mbyA9ICRzdHVkZW50LT5nZXRJbmZvKCk7CgkJJHN0bXQtPmJpbmRQYXJhbSgnOm5hbWUnLCAkdGhpcy0+ZGItPnF1b3RlKCRpbmZvWyduYW1lJ10pKTsKCQkkc3RtdC0+YmluZFBhcmFtKCc6YWdlJywgJHRoaXMtPmRiLT5xdW90ZSgkaW5mb1snYWdlJ10pKTsKCQkkc3RtdC0+YmluZFBhcmFtKCc6ZW1haWwnLCAkdGhpcy0+ZGItPnF1b3RlKCRpbmZvWydlbWFpbCddKSk7CgkJJHN0bXQtPmV4ZWN1dGUoKTsKCX0KCWZ1bmN0aW9uIHNlbGVjdEZyb21EQigpewoJCS8vKioqCgl9Cn0KJHN0dWRlbnQgPSBuZXcgU3R1ZGVudCgkX1BPU1RbJ25hbWUnXSwgJF9QT1NUWydhZ2UnXSwgJF9QT1NUWydlbWFpbCddKTsKJGRiID0gbmV3IFBETygnbXlzcWw6aG9zdD1sb2NhbGhvc3Q7ZGJuYW1lPXRlc3QnLCAncm9vdCcsICcnKTsKJG1hcHBlciA9IG5ldyBEYXRhTWFwcGVyKCRkYik7CiRtYXBwZXItPmluc2VydEludG9EQigkc3R1ZGVudCk7
<?
class Student {
private $properties;
function __construct($name, $age, $email){
$name = strip_tags(trim($name));
$email = strip_tags(trim($email));
$age = strip_tags(trim((int)$age));
$this->properties = array('name'=>$name, 'age'=>$age, 'email'=>$email);
}
function getInfo(){
return $this->properties;
}
}
class DataMapper {
protected $db;
function __construct($db){
$this->db = $db;
}
function insertToDB(Student &$student){
$query = "INSERT INTO tbl (name, age, email) VALUES (:name, :age, :email)";
$stmt = $this->db->prepare($query);
$info = $student->getInfo();
$stmt->bindParam(':name', $this->db->quote($info['name']));
$stmt->bindParam(':age', $this->db->quote($info['age']));
$stmt->bindParam(':email', $this->db->quote($info['email']));
$stmt->execute();
}
function selectFromDB(){
//***
}
}
$student = new Student($_POST['name'], $_POST['age'], $_POST['email']);
$db = new PDO(parse_ini_file('config.ini'));
$mapper = new DataMapper($db);
$mapper->insertToDB($student);