$this 当不在对象上下文中时在另一个 class 中使用数据库 class
$this when not in object context when using database class in another class
我已经查看了其他问题,大多数人说这与调用静态方法等有关,none 其中可以解释为什么我会收到此错误?
我已经习惯了 classes,不想在每个 class 中都构建到数据库的连接,但每个 class 都有一个 class其他使用数据库连接的 class 使用(在长 运行 中它更有意义并且工作更少)。
下面是 2 个 classes,加载 classes 的 init.php 和 index.php 上对 class 的调用最多基本.
我在“报告 class”中指出的那一行引发了错误。
DB 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;
}
}
?>
报告class
<?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>";
?>
这是什么原因造成的?
它是一个静态方法,因此您需要静态访问它或获取该方法所在的实例。按照您目前的设计方式,目前无法做到这一点。无论如何,该方法似乎不应该是静态的,所以我只是让它成为非静态的:
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 更改为:
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;
}
}
我已经查看了其他问题,大多数人说这与调用静态方法等有关,none 其中可以解释为什么我会收到此错误?
我已经习惯了 classes,不想在每个 class 中都构建到数据库的连接,但每个 class 都有一个 class其他使用数据库连接的 class 使用(在长 运行 中它更有意义并且工作更少)。
下面是 2 个 classes,加载 classes 的 init.php 和 index.php 上对 class 的调用最多基本.
我在“报告 class”中指出的那一行引发了错误。
DB 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;
}
}
?>
报告class
<?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>";
?>
这是什么原因造成的?
它是一个静态方法,因此您需要静态访问它或获取该方法所在的实例。按照您目前的设计方式,目前无法做到这一点。无论如何,该方法似乎不应该是静态的,所以我只是让它成为非静态的:
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 更改为:
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;
}
}