dotaer1993 2014-11-19 13:55 采纳率: 100%
浏览 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line