php Illegal offset type in isset or empty

php Illegal offset type in isset or empty

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Illegal offset type in isset or empty

You encounter the error Warning: Illegal offset type in isset or empty when you try to access a PHP array using a string key that isn't valid for that specific data structure, or more commonly, when your code attempts to treat an object as an array. In your case, the stack trace points to Session.php on line 4, inside the exists() method:

return (isset($_SESSION[$name])) ? true : false;

This line is syntactically correct for accessing the global $_SESSION superglobal. However, PHP throws this specific warning when $name is not a simple string or integer. The most frequent cause in modern frameworks and legacy TPL patterns is accidentally passing an object instance instead of its identifier (usually an ID).

Looking at your User.php class, specifically the login() method, there is a subtle but critical flaw that triggers this error upon the next page load.

The Root Cause: Object vs. ID in Session Storage

In your User::login() method, you successfully authenticate the user and then store their session data:

public function login($username = null, $password = null) {
    $user = $this->find($username);

    if($user) {
        if($this->data()->password === Hash::make($password, $this->data()->salt)) {
            Session::put($this->_sessionName, $this->data()->id); // Correct: stores ID
            return true;
        }
    }

    return false;
}

Here, you correctly store $this->data()->id (an integer) into the session. This is good practice because it keeps your session state decoupled from your database schema.

The problem arises in the User::__construct() method. When a user visits index.php, the Session::exists($this->_sessionName) check passes, and you retrieve the stored value:

if(Session::exists($this->_sessionName)) {
    $user = Session::get($this->_sessionName); // Retrieves the ID (e.g., "123")
    
    if($this->find($user)) { ... }
} else {
    $this->find($user); // $user is null here, which is fine for find() usually
}

Wait, looking closely at your User::__construct() logic:

public function __construct($user = null) {
    // ... initialization ...
    
    if(!Session::exists($this->_sessionName)) {
        $this->find($user);
    } else {
        $user = Session::get($this->_sessionName); // $user holds the ID string/int
        
        if($this->find($user)) {
            $this->_isLoggedIn = true;
        }
        
        // Missing: What happens if find() returns false here? 
        // The code comments say "Pr0cess logout"

Our network