$ this变量的致命错误 [英] Fatal Error For $this variable

查看:113
本文介绍了$ this变量的致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个致命错误,说我不在对象上下文中不能使用$this变量:- 严重错误:在第29行的C:\ xampp \ htdocs \ ooplr \ classes \ DB.php的对象上下文中不使用$ this

I get a fatal error saying that I can't use $this variable when not in object context: - Fatal error: Using $this when not in object context in C:\xampp\htdocs\ooplr\classes\DB.php on line 29

本质上,我正在尝试设计一个复杂的用户登录系统.这是DB.php文件的代码:

Essentially, I am trying to design a sophisticated user login system. Here is the code for the DB.php 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(PDOException $e) {
            die($e->getMessage());
        }

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

        }
        return self::$_instance;
    }                   
    public static function query($sql, $params=array()){

        $this->_error = false; <--- Error code comes from this line!
        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;

    }
    private function action($action, $table, $where = array()){
        if(count($where)===3){
            $operators = array('=','>','<','>=','<=');
            $field = $where[0];
            $operator = $where[1];
            $value = $where[2];
        }
        if(in_array($operator, $operators)){
            $sql ="{$action} FROM {$table} WHERE {$field} {$operator} ?";
            if($this->query($sql, array($value))->error()){
                return $this;
            }
        }
        return false;
    }

    public function get($table,$where){
        return $this->action('SELECT*',$table, $where);
    }

    public function delete($table, $where){
        return $this->action('DELETE',$table, $where);
    }

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


}

我将如何解决该问题: $ this-> _ error = false; 以及如何将其放在对象上下文"中?

How would I get to fix that line: $this->_error = false; and how do I put it in "object context"?

推荐答案

您已将query方法声明为静态方法. $this需要引用一个实例,因此您不能在静态方法中使用它.

You've declared your query method as static. $this requires an instance to refer to, so you can't use it in a static method.

您需要正确地实例化您的类,并使用 non -static方法使用$this.

You need to properly instantiate your class, and use non-static methods to use $this.

请注意,您的__construct()方法应声明为公共.

Note that your __construct() method should be declared public.

这篇关于$ this变量的致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆