在另一个类中使用数据库类时不在对象上下文中时$this


$this when not in object context when using database class in another class

我已经查看了其他问题,大多数人说这与调用静态方法等有关,但没有一个能够解释为什么我会收到此错误?

我已经习惯了类,不想在每个类中构建与数据库的连接,而是为它提供一个类,每个使用数据库连接的类的其他类都使用(从长远来看,它更有意义,工作量更少)。

下面是 2 个类,init.php它们加载类和对索引上的类的调用.php所有这些都是最基本的。

这是我在"报告类"中指出的引发错误的行。

数据库类

<?php
class DB{
    
    public static $instance = null;
    
    private $_pdo;
    
    public function __construct(){
        try{
            $this->_pdo = new PDO('mysql:host=12345;dbname=12345', '12345', '12345');
            $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e){
            $e->getMessage();
        }
    }
    
    public static function getInstance() {
        if(!isset(self::$instance)) {
            self::$instance = new DB();
        }
        return self::$instance;
    }
      
}
?>

报告类

<?php
class Reports{
    
     private $_db;
     
     public function __construct(){
         $this->_db = DB::getInstance();
     }
     
     public static function getReports($table, $version, $orderBy){
         
         $sql = "SELECT * FROM {$table} WHERE name = :name ORDER BY :orderBy";
         $query = $this->_db->prepare($sql); // line thats throwing the error on $this->_db
         $query->execute(array(
            ':name'=>$version,
            ':orderBy'=>$orderBy
         ));
         $result = $query->fetchALL(PDO::FETCH_OBJ);
         
         return $result;
     }
}
?>

初始化.php

<?php
error_reporting(E_ALL);
session_start();
session_regenerate_id(true);
function autoload($class) {
    require_once 'classes/' . $class . '.php';
}
spl_autoload_register('autoload');
?>

索引.php

<?php
require_once "core/init.php";
$reports = Reports::getReports("demo", "name1", "id");
echo "<pre>" . print_r($reports) . "</pre>";
?>

是什么原因造成的?

它是一个

静态方法,因此您需要静态访问它或获取该方法所在的实例。按照您目前设计的方式,没有办法做到这一点。无论如何,该方法似乎不应该是静态的,所以我只会让它成为非静态的:

class Reports{
     private $_db;
     public function __construct(){
         $this->_db = DB::getInstance();
     }
     public function getReports($table, $version, $orderBy){
         $sql = "SELECT * FROM {$table} WHERE name = :name ORDER BY :orderBy";
         $query = $this->_db->prepare($sql); // line thats throwing the error on $this->_db
         $query->execute(array(
            ':name'=>$version,
            ':orderBy'=>$orderBy
         ));
         $result = $query->fetchALL(PDO::FETCH_OBJ);
         return $result;
     }
}

用法:

require_once "core/init.php";
$report = new Reports();
$reports = $report->getReports("demo", "name1", "id");
echo "<pre>" . print_r($reports) . "</pre>";

如果您设置为保持静态,那么您getReports方法将需要执行以下操作:

     public static function getReports($table, $version, $orderBy){
         $reports = new self();
         $sql = "SELECT * FROM {$table} WHERE name = :name ORDER BY :orderBy";
         $query = $reports->_db->prepare($sql); // line thats throwing the error on $this->_db
         $query->execute(array(
            ':name'=>$version,
            ':orderBy'=>$orderBy
         ));
         $result = $query->fetchALL(PDO::FETCH_OBJ);
         return $result;
     }

将报告类更改为:

class Reports{
     public static function getReports($table, $version, $orderBy){
         $db = DB::getInstance();
         $sql = "SELECT * FROM {$table} WHERE name = :name ORDER BY :orderBy";
         $query = $db->prepare($sql);
         $query->execute(array(
            ':name'=>$version,
            ':orderBy'=>$orderBy
         ));
         $result = $query->fetchALL(PDO::FETCH_OBJ);
         return $result;
     }
}