How can encapsulating access to the $_SESSION superglobal using classes improve code maintainability in PHP projects?

Encapsulating access to the $_SESSION superglobal using classes improves code maintainability by providing a centralized location for interacting with session data. This allows for easier debugging, testing, and modification of session-related functionality without directly manipulating the superglobal array throughout the codebase.

class SessionManager {
    public function setSessionValue($key, $value) {
        $_SESSION[$key] = $value;
    }

    public function getSessionValue($key) {
        return $_SESSION[$key] ?? null;
    }

    public function unsetSessionValue($key) {
        unset($_SESSION[$key]);
    }
}

// Example usage
$sessionManager = new SessionManager();
$sessionManager->setSessionValue('user_id', 123);
$userID = $sessionManager->getSessionValue('user_id');
$sessionManager->unsetSessionValue('user_id');