$ this当不在对象的上下文 [英] $this when not in object's context

查看:137
本文介绍了$ this当不在对象的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


致命错误:在
中不使用对象上下文时使用$ this C: \ xampp \htdocs\practice\classes\pagination.php on line 20


我的工作流程如下: / p>

从pagination2.php页面我初始化了所有的类实例:

  require_once'../core/init.php'; 


$ pagination = new Pagination();
$ user = new User();
$ data = $ user-> data();

$ pagenum = $ pagination :: getAllContent('userpost',$ data-> user_id);
echo $ pagenum;

pagination.php页面包含分页类

  class Pagination {
private $ _db;
public function __construct(){
$ this-> _db = DB :: getInstance();
print_r($ this-> _db);
}

public static function getUserContentOffset($ table,$ offset,$ id){
$ val = array();
$ val [] = $ id;
$ pointerDB = $ this-> _db-> fetchPostByOffset($ table,$ offset,$ val);
return $ pointerDB-> results();
}
public static function getAllContent($ table,$ id){
$ val = array();
$ val [] = $ id;
print_r($ val);
$ pointerDB = $ this-> _db-> fetchAllPost($ table,$ val); // error is here
return $ pointerDB-> countit();

}



}

$ this-> _db 应该包含 DB类的实例。我调用DB类的方法使用 $ this-> _db



DB.php

p>

  class DB {

private static $ _instance = null;
private $ _pdo,$ _ query,$ _ error = false,$ _ results,$ _ count = 0;
public 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 function fetchAllPost($ table,$ val = array()){
$ sql =SELECT title FROM。$ table。WHERE users_user_id =? ;
return $ this-> query($ sql,$ val);
}
}


解决方案

code> $ this 在静态方法中不可用。静态方法没有与它们相关联的任何对象,因此在静态方法中没有可用的 $ this 引用。因此,您可以声明一个静态属性,并在 static 函数中使用它。现在,调用 DB 实例应该在静态属性上,以便您可以在静态函数中访问它。

  class Pagination {
private static $ _db;
public function __construct(){
self :: $ _ db = DB :: getInstance();
}

public static function getUserContentOffset($ table,$ offset,$ id){
$ val = array();
$ val [] = $ id;
$ pointerDB = self :: $ _ db-> fetchPostByOffset($ table,$ offset,$ val);
return $ pointerDB-> results();
}
public static function getAllContent($ table,$ id){
$ val = array();
$ val [] = $ id;
$ pointerDB = self :: $ _ db-> fetchAllPost($ table,$ val); // error is here
return $ pointerDB-> countit();
}
}

现在你可以这样尝试

  $ pagintation = new Pagination(); 
$ pagenum = $ pagination-> getAllContent('userpost',$ data-> user_id);

希望它有帮助。


I am getting the following error:

Fatal error: Using $this when not in object context in C:\xampp\htdocs\practice\classes\pagination.php on line 20

My workflow is the following:

From pagination2.php page i initialized all the class instances:

  require_once '../core/init.php';


  $pagination=new Pagination();
  $user=new User();
  $data=$user->data();

  $pagenum=$pagination::getAllContent('userpost',$data->user_id);
  echo $pagenum;

pagination.php page contains pagination class

class Pagination{
    private $_db;
    public function __construct(){
       $this->_db=DB::getInstance();
       print_r($this->_db);
    }

    public static function getUserContentOffset($table,$offset,$id){
        $val=array();
        $val[]=$id;
        $pointerDB=$this->_db->fetchPostByOffset($table,$offset,$val);
        return $pointerDB->results();
    }
    public static function getAllContent($table,$id){
        $val=array();
        $val[]=$id;
        print_r($val);
        $pointerDB=$this->_db->fetchAllPost($table,$val); // error is here
        return $pointerDB->countit();

    }



    }

$this->_db should bear the instance of DB class.Then why i am getting the error when i call a method of DB class using $this->_db

DB.php

class DB{

        private static $_instance=null;
    private $_pdo,$_query,$_error=false,$_results,$_count=0;
    public 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 function fetchAllPost($table,$val=array()){
            $sql="SELECT title FROM ".$table." WHERE users_user_id = ?";
            return $this->query($sql,$val);
        }
}

解决方案

$this is not available in static methods. Static Methods don't have any Object associated with them, hence there is no $this reference available inside a static method. So you can declare a static properties and use it in your static function. Now your calling DB instance should be on static property so that you can access it in your static functions.

class Pagination{
     private static $_db;
     public function __construct(){
        self::$_db = DB::getInstance();
     }

    public static function getUserContentOffset($table,$offset,$id){
       $val=array();
       $val[]=$id;
       $pointerDB=self::$_db->fetchPostByOffset($table,$offset,$val);
       return $pointerDB->results();
    }
    public static function getAllContent($table,$id){
      $val=array();
      $val[]=$id;
      $pointerDB = self::$_db->fetchAllPost($table,$val); // error is here
      return $pointerDB->countit();
   }
}

Now you can try it this way

$pagintation = new Pagination();
$pagenum = $pagination->getAllContent('userpost',$data->user_id);

Hope it helps.

这篇关于$ this当不在对象的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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