What are the potential pitfalls of using global variables in a PHP database class for accessing variables?

Using global variables in a PHP database class can lead to potential issues such as namespace collisions, difficulty in tracking variable changes, and decreased code readability. To avoid these pitfalls, it is recommended to use class properties to store variables within the class scope.

class Database {
    private $host;
    private $username;
    private $password;
    private $database;

    public function __construct($host, $username, $password, $database) {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
    }

    public function connect() {
        $connection = new mysqli($this->host, $this->username, $this->password, $this->database);
        // Connect to the database
        return $connection;
    }
}

// Usage
$database = new Database('localhost', 'username', 'password', 'database');
$connection = $database->connect();