I'm currently learning the PSR-2 standard, the namespace has me confused as it's causing the following error in my browser, ** Fatal error: Cannot declare class Database\\Database because the name is already in use in /var/www/html/config/database.php on line 6** if anyone could help me I would really appreciate it, thank you very much
Edit: I'm getting this new error, ** Fatal error: Uncaught Error: Class 'Database\\PDO' not found in /var/www/html/config/database.php:18 Stack trace: #0 /var/www/html/login.php(19): Database\\Database->getConnection() #1 {main} thrown in /var/www/html/config/database.php on line 18**
<?php
namespace Database;
use Database;
class Database
{
private $host = "";
private $db_name = "";
private $username = "";
private $password = "";
public $conn;
public function getConnection()
{
$this->conn = null;
try {
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
You are in the Database
namespace. So your new PDO
is looking for Database\\PDO
.
To get around this you need to use PDO from the root namespace by using new \\PDO
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.