How to fix the __PHP_Incomplete_Class Object error in PHP $_SESSION’s

I ran into a problem recently where I was instantiating an object and passing it login credentials in order to authenticate a user. If the users email address and password were correct my method will return me an object containing all of an individual users information that’s important to their $_SESSION so it can persist across pages, as you normally want in an authenticated system.

Note: I have not tested this with normal arrays. I have only tested this with the $_SESSION superglobal array.

The __PHP_Incomplete_Class Error

If you initialize a session (session_start) before you have instantiated the object and then you assign the object to a $_SESSION variable then you print_r($_SESSION) you will receive this error.

__PHP_Incomplete_Class Object (
[__PHP_Incomplete_Class_Name] => user
[user] => __PHP_Incomplete_Class Object (

No bueno

)

Here’s a code example

session_start();
$user = user::authenticate($email, $password);

if ($user !== false) {
$_SESSION['user'] = $user;
}

// __PHP_Incomplete_Class
print_r($_SESSION['user']);

This will give you a similar error to the example provided above. Don’t ask me why.  I haven’t gone balls deep (forgive the crude analogy) in the parser code to analyze the origination of this issue, I just know how to fix it.

Fix the __PHP_Incomplete_Class Error

If you instantiate the object before you initialize the session then you’re golden.  Take this code in its for instance:

$user = user::authenticate($email, $password);

if ($user !== false) {
session_start();

$_SESSION['user'] = serialize($user);
}

// Fixed!
print_r($_SESSION);

P.S. You  want to turn  session.auto_start OFF in the php.ini
If you don’t know how to do this make sure in your php.ini that session.auto_start = Off.

That’s basically it. If you have any questions, feel free to post in the comments section.

Your welcome,
bryan

Related reading

6 Responses to “How to fix the __PHP_Incomplete_Class Object error in PHP $_SESSION’s”

  1. cheruvattil  on October 30th, 2009

    we can resolve this by giving a reference to the class which we are trying to unserialize.

    class TwitterTweets
    {
    var $tweets;
    var $image;
    var $accName;
    function __construct($tweet, $name, $image)
    {
    $this->tweets = $tweet;
    $this->image = $image;
    $this->accName = $name;
    }
    }
    // if we are not having any reference we need to give a erference
    // $tweetRaw['tweets'] – serialized class
    $serTweetObj = unserialize($tweetRaw['tweets']);

  2. outis  on December 15th, 2009

    Normally, you wind up with an __PHP_Incomplete_Class in $_SESSIONS when the *class* isn’t defined before calling session_start(), rather than when an object isn’t instantiated. The reason is PHP unserializes everything in the session storage; if a class isn’t defined, it unserializes it into a __PHP_Incomplete_Class.

    I suspect the reason the fix worked was because you also first serialized the user, and thus unserialize it, probably after the class is defined. You’re getting the __PHP_Incomplete_Class error when accessing $_SESSION['user'], rather than in the script that performs the authentication, correct?

  3. bryan  on December 16th, 2009

    It’s just annoying.

  4. Originative  on January 1st, 2010

    in my experience PHP sucks… it is not for programmers it is for children to play with…….

  5. Pies  on March 16th, 2010

    The easiest workaround is to define my __autoload() function before session_start(). This way the appropriate classes are loaded when needed.

  6. PHP Autoloader  on May 16th, 2010

    As mentioned above. An Autoloader implementation will repair this error.


Leave a Reply