Php 类…中未定义的变量;但我定义它

Php 类…中未定义的变量;但我定义它,php,mysql,database,Php,Mysql,Database,我正在尝试为我的数据库创建一个类:类结构如下 <?php require_once("config.php"); // include(Username, passowrd, server and databaseName) class MySQLDatabase{ private $connection; function __construct(){ $this->openConnection();

我正在尝试为我的数据库创建一个类:类结构如下

<?php
    require_once("config.php"); // include(Username, passowrd, server and databaseName)
    class MySQLDatabase{
        private $connection;

        function __construct(){
            $this->openConnection();
        }

        public function openConnection(){
            $this->$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PWD, DB_NAME);
            if(!$connection){
                die("Database connection failed: ".mysql_error);
            }
        }

    }
    $database = new MySQLDatabase();
    $db =&$database;

 ?>
    <?php                   
require("../includes/database.php");                                       if(isset($database)){
echo "true";
}else{
echo "false";
}
    ?>

$this::$connection
(保留
$
)将引用静态变量。因为您有一个类变量,所以应该通过
$this->connection


您还需要在
if
-语句中使用相同的指令。

$this::$connection
(保留
$
)将引用静态变量。因为您有一个类变量,所以应该通过
$this->connection


您还需要在
if
-语句中使用相同的指令。

$this::$connection
(保留
$
)将引用静态变量。因为您有一个类变量,所以应该通过
$this->connection


您还需要在
if
-语句中使用相同的指令。

$this::$connection
(保留
$
)将引用静态变量。因为您有一个类变量,所以应该通过
$this->connection



您还需要在
if
-语句中使用相同的说明。

您使用的是
$this->$connection
而不是
$this->connection
。删除
$
登录
连接
,然后重试。

您使用的是
$this->$connection
而不是
$this->connection
。删除
$
登录
连接
,然后重试。

您使用的是
$this->$connection
而不是
$this->connection
。删除
$
登录
连接
,然后重试。

您使用的是
$this->$connection
而不是
$this->connection
。删除
$
登录
连接
并重试。

在该函数的上下文中,如果(!$this->connection)不是
如果(!$connection)
mysql\u错误
将发出有关未定义常量的通知。您的意思是使用
mysqli\u connect\u error()
对于mysqli在该函数上下文中发出的最后一个连接错误,它应该是
如果(!$this->connection)
而不是
如果(!$connection)
mysql\u error将发出关于未定义常量的通知。您的意思是使用
mysqli\u connect\u error()
对于mysqli在该函数上下文中发出的最后一个连接错误,它应该是
如果(!$this->connection)
而不是
如果(!$connection)
mysql\u error将发出关于未定义常量的通知。您的意思是使用
mysqli\u connect\u error()
对于mysqli在该函数上下文中发出的最后一个连接错误,它应该是
如果(!$this->connection)
而不是
如果(!$connection)
mysql\u error将发出关于未定义常量的通知。您的意思是使用
mysqli\u connect\u error()
来处理mysqli发出的最后一个连接错误。我不明白为什么这是公认的答案,因为它与静态变量无关@EmCo的回答似乎更合适。你是对的,我指定了错误的操作员。对于静态变量,它应该是
$this::$connection
或更好的
self::$connection
。我会更新我的答案。我不明白为什么这是公认的答案,因为它与静态变量无关@EmCo的回答似乎更合适。你是对的,我指定了错误的操作员。对于静态变量,它应该是
$this::$connection
或更好的
self::$connection
。我会更新我的答案。我不明白为什么这是公认的答案,因为它与静态变量无关@EmCo的回答似乎更合适。你是对的,我指定了错误的操作员。对于静态变量,它应该是
$this::$connection
或更好的
self::$connection
。我会更新我的答案。我不明白为什么这是公认的答案,因为它与静态变量无关@EmCo的回答似乎更合适。你是对的,我指定了错误的操作员。对于静态变量,它应该是
$this::$connection
或更好的
self::$connection
。我会更新我的答案。
on line 9
Call Stack
#   Time    Memory  Function    Location
1   0.0005  240792  {main}( )   ..\index.php:0
2   0.0008  249872  require( 'C:\wamp\www\waytoits\includes\database.php' ) ..\index.php:3
3   0.0013  250528  MySQLDatabase->__construct( )   ..\database.php:55
4   0.0013  250592  MySQLDatabase->openConnection( )    ..\database.php:15

( ! ) Fatal error: Cannot access empty property in C:\wamp\www\waytoits\includes\database.php on line 9
Call Stack
#   Time    Memory  Function    Location
1   0.0005  240792  {main}( )   ..\index.php:0
2   0.0008  249872  require( 'C:\wamp\www\waytoits\includes\database.php' ) ..\index.php:3
3   0.0013  250528  MySQLDatabase->__construct( )   ..\database.php:55
4   0.0013  250592  MySQLDatabase->openConnection( )    ..\database.php:15