class Database {
private $host = "localwhost"; // intended typo
private $user = "root";
private $password = "";
private $name = "filmy";
private $connection;
function connect() {
$this->connection = @ new mysqli($this->host, $this->user, $this->password, $this->name);
if($this->connection->connect_errno) {
var_dump($this->connection->connect_errno);
exit("<h1>Database connection error: $this->connection->connect_errno</h1>");
}
}
}
$database = new Database();
$database->connect();
When I try to call $database->connect();
I got this fatal error:
Fatal error: Uncaught Error: Object of class mysqli could not be converted to string in C:\xampp\htdocs\database.php:13 Stack trace: #0 C:\xampp\htdocs\database.php(23): Database->connect() #1 {main} thrown in C:\xampp\htdocs\database.php on line 13
The var_dump
says $this->connection->connect_errno
is int(2002)
, not a mysqli
object.
CodePudding user response:
You need to use the complex form of variable interpolation to perform double indirection of an object.
exit("<h1>Database connection error: {$this->connection->connect_errno}</h1>");