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
