How can Object-Oriented principles be applied to improve the handling of global data in PHP scripts?

Global data in PHP scripts can lead to issues with maintainability and potential conflicts between different parts of the code. One way to improve this is by encapsulating the global data within a class using Object-Oriented principles. By creating a class that manages the global data and provides methods to access and modify it, we can ensure better organization and control over the data.

class GlobalDataHandler {
    private static $globalData = [];

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

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

GlobalDataHandler::setData('username', 'JohnDoe');
echo GlobalDataHandler::getData('username');