编辑:那是更多的调用代码:
$worker = new Worker(); // :|
$worker->addWorker($_POST);
echo 'Post vars:<br>';
var_dump($_POST);
echo 'Object Worker parameters:<br>';
echo $worker->name;
get_object_vars(serialize($worker));
echo 'create<br>';
var_dump(serialize($worker));
$_SESSION['save_confirm'] = serialize($worker);
看起来像这样: var_dump 正确显示:
array(8) { ["name"]=> string(4) "Name" ["surname"]=> string(7) "Surname" ["dob"]=> string(10) "dd/mm/yyyy " ["skills"]=> string(3) "ERN" ["postcode"]=> string(8) "Postcode" ["street"]=> string(6) "Street" ["email"]=>字符串(5)“电子邮件”[“动作”]=>字符串(6)“创建”}
虽然 get_object_vars 不起作用,但 echo $worker->name 给出了正确的值。,这个对象中有 PDO 语句,但也有:
public function __sleep () { unset($conn); }
编辑:类:
class Worker extends Core {
public $name;
public $surname;
public $dob;
public $skills;
public $postcode;
public $street;
public $email;
public $tel;
public $erefnumber;
// Adding worker to object
public function addWorker( array $postArray)
{
$this->name = $postArray['name'];
$this->surname = $postArray['surname'];
$this->dob = $postArray['dob'];
$this->skills = $postArray['skills'];
$this->postcode = $postArray['postcode'];
$this->street = $postArray['street'];
$this->email = $postArray['email'];
$this->tel = $postArray['tel'];
$this->erefnumber = $postArray['erefnumber'];
}
//Saving worker data to database, need provide group name (table name)
public function saveWorker($group) {
if(isset($this->conn)) {
try
{
parent::__construct();
$this->conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //catch exceptions
$q = query('INSERT INTO :group(name, surname, dob, skills, postcode, street, email, tel, erefnumber) VALUES(
:name,
:surname,
:dob,
:skills,
:postcode,
:street,
:email,
:tel,
:erefnumber)'); //sql query with group name
$query = $pdo->prepare($q);
$stmt -> bindValue(':group', $group, PDO::PARAM_STR);
$stmt -> bindValue(':name', $name, PDO::PARAM_STR);
$stmt -> bindValue(':surname', $surname, PDO::PARAM_STR);
$stmt -> bindValue(':dob', $dob, PDO::PARAM_STR);
$stmt -> bindValue(':skills', $skills, PDO::PARAM_STR);
$stmt -> bindValue(':postcode', $postcode, PDO::PARAM_STR);
$stmt -> bindValue(':street', $street, PDO::PARAM_STR);
$stmt -> bindValue(':email', $email, PDO::PARAM_STR);
$stmt -> bindValue(':tel', $tel, PDO::PARAM_STR);
$stmt -> bindValue(':erefnumber', $erefnumber, PDO::PARAM_STR);
$results = $query->execute();
}
catch(PDOException $e)
{
return 'There was some error: ' . $e->getMessage();
}
}
else {
echo 'no connection (saveWorkers)';
}
unset($stmt);
return var_dump($list);
//no exceptions
}
class Core {
public $conn;
public function __construct() {
$this->dbConnect();
}
public function dbConnect() {
$host = 'localhost';
$port = '3307';
$username = 'modium_test';
$password = 'test';
$database ='modium_test';
try{
$this->conn = new PDO('mysql:host='.$host.';dbname='.$database.';port='.$port, $username, $password );
echo 'Connection successful!';
echo var_dump($this->conn);
}
catch(PDOException $e){
echo 'Error: ' . $e->getMessage();
}
}
public function __sleep () {
unset($conn);
}
}