dotaer1993
2014-11-19 13:55
浏览 56
已采纳

致命错误:在第98行的C:\ wamp \ www \ it-fix.nu \ login \ classes \ user.php中的非对象上调用成员函数first()

here are my DB file:

<?php 
class DB {
private static $_instance = null;
private $_pdo, 
        $_query, 
        $_error = false, 
        $_results,
        $_count = 0;

private function __construct() {
    try {
        $this->_pdo = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
    } catch(PDOExeption $e) {
        die($e->getMessage());
    } 
}

public static function getInstance() {
    if(!isset(self::$_instance)) {
        self::$_instance = new DB();
    }
    return self::$_instance;
}

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;
}

public function results() {
    return $this->_results;
}

public function first() {
    return $this->results()[0];
} 

public function error() {
    return $this->_error;
}

public function count() {
    return $this->_count;
}
} 
?>

and my user file:

<?php 
class User {
private $_db,
        $_data,
        $_sessionName,
        $_cookieName,
        $_isLoggedIn;

public function __construct($user = null) {
    $this->_db = DB::getInstance();

    $this->_sessionName = Config::get('session/session_name');
    $this->_cookieName = Config::get('remember/cookie_name');

    if (!$user) {
        if (Session::exists($this->_sessionName)) {
            $user = Session::get($this->_sessionName);

            if ($this->find($user)) {
                $this->_isLoggedIn = true;
            }   else {
                //process logout
            }
        }
    } else {
        $this->find($user);
    }

}



//this is the function, where it gives me the error

public function hasPermission($key) {
    $group = $this->_db->get('groups', array('id', ' = ', $this->data()->group));
    print_r($group->first()); // <--- on this line
}

public function exists() {
    return (!empty($this->_data)) ? true : false;
}
}
?>

the error come from the hasPermission() method.

as i see it, it looks like, it does'nt return a object from the database, but in the DB filem it clearly states in the query() method, that it should return as PDO::FETCH_OBJ

PS: if cut off alot of code, that are not important in this problem, feel free to ask fro more code.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhanbi0027 2014-11-19 14:14
    已采纳

    This is exactly what error message says. On of your variable is not an object, what you want to use as an object.

    One of it will not be an object.

    function hasPermission($key) {
        echo 'DB <br />';
        var_dump($this->_db);
    
        echo 'Data<br />';
        var_dump($this->$this->data());
    
        //Debug, is this successfull!
        $group = $this->_db->get('groups', array('id', ' = ', $this->data()->group));
        echo 'group<br />';
        var_dump($group); //<!-- This will be not an object
        print_r($group->first()); // <--- on this line
    }
    

    Show us the $this->data() method.

    打赏 评论

相关推荐 更多相似问题