編集:それはより多くの呼び出しコードです:
$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 "["スキル"]=>string(3)" ERN "[" postcode "] => string(8)" Postcode "[" street "] => string(6)" Street "[" email "] => string(5) "Email" ["action"] => string(6) "create"}
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);
}
}