致命错误:在第47行的C:\ xampp \ htdocs \ lib \ user.php中的非对象上调用成员函数findOne()

时间:2013-11-19 03:14:09

标签: mongodb

<?php

require_once('dbconnection.php');

require_once('session.php');

class User {
    const COLLECTION = 'users';

    private $_mongo;
    private $_collection;
    private $_user;
    public function _construct()
    {
        $this->_mongo = DBConnection::instantiate(); 
        $this->_collection = $this->_mongo->getCollection(User::COLLECTION);

        if ($this->isLoggedIn()) $this->_loadData();

    }

    public function isLoggedIn()
    {
        return isset($_SESSION['user_id']);
    }

    public function authenticate($username, $password)
    {

$query = array(
'username' => $username,
'password' => md5($password)
);
$this->_user = $this->_collection->findOne($query); if (empty($this->_user)) return False;
$_SESSION['user_id'] = (string) $this->_user['_id'];
return True;
}

public function logout()
{
    unset($_SESSION['user_id']);
}

public function _get($attr)
{
    if (empty($this->_user))
        return Null;
    switch($attr) {
        case 'address':
            $address = $this->_user['address'];
            return sprintf('Town: %s, Planet: %s', $address['town'], $address['planet']);
        case 'town':
            return $this->_user['address']['town'];
        case 'planet':
            return $this->_user['address']['planet'];
        case 'password':
            return NULL;
        default:
            return (isset($this->_user[$attr])) ?
                $this->_user[$attr] : NULL;
    }

}

private function _loadData()
{
    $id = new MongoId($_SESSION['user_id']);
    $this->_user = $this->_collection->findOne(array('_id' => $id));
}
}

1 个答案:

答案 0 :(得分:1)

$ _ collection尚未正确初始化。它在__construct()方法中设置,但似乎失败了。错误消息不是指这个,所以不清楚出了什么问题。