';无法访问空属性';php中的错误


'cannot access empty property' error in php

我对以下php代码中的错误感到困惑。尽管如此,我在代码上看了很多次,但都找不到为什么会出现这个错误"无法访问空属性"

class DBTest{
//declare variables
private $servername = "localhost";
private $username = "root";
private $password = "";
private $database = "avn_test";
private static $conn;
private $results;
//constructor
public function __construct(){
self::$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit();}
} //close constructor
public function executeQuery($query='') {
if(!empty($query)){
$query = self::$conn->real_escape_string($query);

此行错误:

$this->results=self::$conn->query($query)或die("数据库错误connection".self::$conn->$error);

if( $this->results->num_rows > 0 ) {
$rowqry = array();
while($row = $this->results->fetch_object()) {
$rowqry[]= $row; } //close of while
$rarray['returnvar'] = $rowqry;
return $rarray;
} else {
return false; } // close of else
}//close of top if
else
return false;
} //close of function
function __destruct(){
self::$conn->close();}
} //close of class
//create an object of class DBTest
$test = new DBTest();
$q= "select * from test";
$tmp = $test->executeQuery($q);
if($tmp){
foreach($tmp as $key => $value){
echo $value;}
}
else
echo 'tmp var is empty';

在行中:

$this->results = self::$conn->query($query) or die("Error in database connection".self::$conn->$error);

self::$conn->$error替换为self::$conn->error

访问静态属性时需要$,但实例属性不需要。

$conn函数中不需要$

self::$conn replace with self::conn
                           ^^^^^^