How can global variables, like a database connection, be accessed within a PHP class or object?

To access global variables, like a database connection, within a PHP class or object, you can use the `global` keyword to bring the variable into the scope of the class or object method. This allows you to access the global variable within the class or object without having to pass it as a parameter.

// Global database connection variable
$connection = new mysqli('localhost', 'username', 'password', 'database');

class MyClass {
    public function someMethod() {
        global $connection;
        
        // Access the global database connection within the class method
        $result = $connection->query("SELECT * FROM table");
        // Rest of the code
    }
}

// Create an instance of the class
$myClass = new MyClass();
$myClass->someMethod();