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

时间:2015-01-30 14:58:04

标签: php pdo

我已经查看了其他问题,并且大多数人都说它与调用静态方法有关,等等......而且没有一个能让我理解为什么我会收到这个错误?

我正在使用类,并且不希望必须在每个类中构建与数据库的连接,但是有一个类,它使用与数据库连接的每个其他类使用它(它只是生成更多从长远来看,感觉和减少工作。)

下面是两个类,init.php加载类和对index.php上的类的调用都是最基本的。

我在“Reports class”中指出的那句话就是抛出错误。

数据库类

<?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;
     }
}
?>

的init.php

<?php
error_reporting(E_ALL);
session_start();
session_regenerate_id(true);

function autoload($class) {
    require_once 'classes/' . $class . '.php';
}

spl_autoload_register('autoload');
?>

的index.php

<?php
require_once "core/init.php";

$reports = Reports::getReports("demo", "name1", "id");

echo "<pre>" . print_r($reports) . "</pre>";
?>

任何指针都会非常感激。

2 个答案:

答案 0 :(得分:1)

它是一个静态方法,因此您需要静态访问它或获取方法所在的实例。它是你目前设计的方式,没有办法做到这一点。似乎那个方法应该是静态的,所以我只是把它变成非静态的:

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;
     }

答案 1 :(得分:0)

将您的报告类更改为:

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;
     }
}