Are there best practices or guidelines for handling global variables in PHP to mitigate security risks?

Global variables in PHP can pose security risks as they can be accessed and modified from anywhere in the code, making it difficult to track their usage and potential vulnerabilities. To mitigate these risks, it is recommended to limit the use of global variables and instead encapsulate data within classes or functions where possible.

class GlobalData {
    private static $data = [];

    public static function setData($key, $value) {
        self::$data[$key] = $value;
    }

    public static function getData($key) {
        return isset(self::$data[$key]) ? self::$data[$key] : null;
    }
}

// Usage example
GlobalData::setData('user_id', 123);
$user_id = GlobalData::getData('user_id');