What are some common pitfalls to avoid when assigning values to session variables in PHP classes, such as in constructors or destructors?

One common pitfall to avoid when assigning values to session variables in PHP classes is not properly initializing the session before trying to access or set session variables. To avoid this issue, always start the session at the beginning of your script using `session_start()`. Additionally, make sure to check if the session is already started before trying to start it again.

// Initialize session if not already started
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

class MyClass {
    public function __construct() {
        // Assign a value to a session variable
        $_SESSION['my_variable'] = 'my_value';
    }
    
    public function __destruct() {
        // Unset the session variable
        unset($_SESSION['my_variable']);
    }
}