Cannot Access Empty Property in PHP

php

Solution

You appear to have an error on line 21. Change:

$this->$password

to:

$this->password

For reference, this is is known as a variable variable. Example:

$a = 'b';
$b = 'I am a variable variable';
echo $$a; // equivalent to `echo $b;` prints 'I am a variable variable'

Problem

I am getting the following error with the following code when attempting to create an instance in another file. I am new to PHP, and can't find any syntax errors with my code. Fatal error: Cannot access empty property in /home/leondash/public_html/poll/classes/database.php on line 21 ``` class Database { private $host, $username, $password, $database; private $db; /** Opens a connection the database */ public function __construct() { $this->host = "localhost"; $this->username = "dbuser"; $this->password = "dbpass"; $this->database = "dbname"; // the following line is giving the error $this->db = mysqli_connect($this->host, $this->username, $this->$password, $this->database) or die('Error: Could not connect to the database'); return true; } } ```

Original source

Related problems