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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵