PHP不完整的类OOP

时间:2013-12-05 11:56:01

标签: php mysql oop

你好我有这个代码

public function query($sql, $params = array()) {

        $this->_error = false;

        if($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if(count($params)) {
                foreach($params as $param) {
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }

        return $this;
    }

当我运行它时,它表示我在行中有错误

$this->_query->bindValue($x, $param);

错误消息说 可捕获的致命错误:类__PHP_Incomplete_Class的对象无法转换为字符串 请帮忙 谢谢

修改

$ params的var_dump

array(1) { 
    [0]=> object(__PHP_Incomplete_Class)#1 (6) { 
        ["__PHP_Incomplete_Class_Name"]=> string(4) 
        "User" ["_db":"User":private]=> object(__PHP_Incomplete_Class)#2 (2) {
            ["__PHP_Incomplete_Class_Name"]=> string(2) "DB" 
            ["connection"]=> NULL 
        } 
        ["_sessionName":"User":private]=> string(4) "user" 
        ["_cookieName":"User":private]=> string(4) "hash" 
        ["_data":"User":private]=> object(stdClass)#3 (7) { 
            ["id"]=> string(3) "144" 
            ["username"]=> string(5) "admin" 
            ["password"]=> string(64) "0611affa6664e471b939cd3197b49e0c3b47d146fc12a472c4275dbd85a7cd67" 
            ["salt"]=> string(32) "458a0dbfbd9bdca381e50b8d753329ea" 
            ["name"]=> string(12) "Artur Papyan" 
            ["joined"]=> string(19) "2013-11-29 07:41:54" 
            ["group"]=> string(1) "1" 
        } 
        ["_isLoggedIn":"User":private]=> bool(true) 
    }
} 

1 个答案:

答案 0 :(得分:0)

错误说清楚:$params至少包含一个__PHP_Incomplete_Class对象,显然无法将其转换为字符串以填充查询。

您获取__PHP_Incomplete_Class个对象的原因是由于会话存储对象的未序列化,该类尚未加载此类。因此,您必须在会话开始之前包含会话存储的类。如果您需要我的建议,自动加载是加载clases的最佳方式,因此您可以完全忘记include / require语句。您可以在此处找到更多信息:http://www.php.net/manual/en/language.oop5.autoload.php