Get_object_vars($ worker)不起作用,而$ object->值起作用

时间:2012-12-02 21:08:34

标签: php object pdo oop

编辑:更多的是调用代码:

$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”] =&gt; string(4)“Name”[“surname”] =&gt; string(7)“Surname”[“dob”] =&gt; string(10)“dd / mm / yyyy”[“skills”] =&gt; string(3)“ERN”[“postcode”] =&gt; string(8)“Postcode”[“street”] =&gt; string(6)“Street”[“email”] =&gt; string(5)“Email”[“action”] =&gt; string(6)“create”}

当get_object_vars不起作用时,echo $ worker-&gt; name给出propper值。  ,在这个对象中有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);
    }
}

2 个答案:

答案 0 :(得分:1)

我猜测$ worker对象的类属性是受保护的还是私有可见性。做一个

var_dump($worker);

如果是这种情况,get_object_vars()将无法看到它们,因为在课堂外无法访问它们。

答案 1 :(得分:1)

您没有将对象传递给get_object_vars,而是传递一个字符串。

get_object_vars(serialize($worker));

应该是

$object_vars = get_object_vars($worker);

现在var_dump($object_vars);将显示您想要的内容。