PHP _Php_Incomplete_Class Object with My $_Session Data

__PHP_Incomplete_Class Object even though class is included before session started

To store an object in a session two things must happen:

1) The class must be defined before the session is started.
2) The class must implement serializable.

forcing access to __PHP_Incomplete_Class object properties

This issue appends when you un serialize an object of a class that hasn't been included yet.
For exemple, if you call session_start before include the class.

A PHPIncompleteClass object can't be accessed directly, but it's ok with foreach, serialize and gettype.
Calling is_object with an PHPIncompleteClass object will result false.

So, if you find a '__PHP_Incomplete_Class' object in your session and you've included your class after the session_load, you can use this function :

function fixObject (&$object)
{
if (!is_object ($object) && gettype ($object) == 'object')
return ($object = unserialize (serialize ($object)));
return $object;
}

This will results a usable object :

fixObject($_SESSION['member']);

Other reasons for __PHP_Incomplete_Class

There is a pretty good duplicated mentioned in the comments and as I commented, you might want to use serialize() here, however I find rarely a good reason to save objects in session for further use.

I'd rather have stored in the session the things I do need in order to create the objects and then from a single entry point to register the necessary objects. Of, use a database to save the needed fields and only store an identifier in the session.

Let's say you need information for the current logged user through all of you application, after successful login. Instead of assigning the instance of the User class into a session, I'd store its details into session, and construct the object each time a new page is requested.

It may add an additional overhead if you store the identifier and query the DB to take all the other info for the user upon each refresh, but still I would go for it, if I really don't see a significant application slowing.

main_entry_point.php:

if (isset($_SESSION['user_id'])) {
$row = $db->query("SELECT name, email, age, role_id FROM users WHERE id = " . $_SESSION['user_id']);
$user = new User($row['name'], $row['email'], $row['age'], $row['role_id']);
}

There can be a lot of further sugar here, like injecting the user instance to your application class if you have one, etc, etc, so you will not work with global variables ($user in this case is a pseudo-global variable for me, since you have a main entry point which is included on each refresh, like index.php in MVC frameworks), but that's not relevant here, the point was in long story short - to register your cart object from the particular things stored in the session.

PHP problem with __php_incomplete_class

The class definition had not been loaded, when PHP tried to deserialize the object in the session.

You can solve your problem by employing Autoloading.

__PHP_Incomplete_Class_Name wrong

Hakre's suggestion to look at session_write_close led me to what appears to be a reliable fix:

register_shutdown_function('session_write_close');

This forces the session to be written out before any memory cleanup and class unloading occurs. This is important due to a change in PHP that can result in a race condition between APC removing class references and PHP writing out the session data: http://news.php.net/php.internals/46999

Get the results into a session variable causing __PHP_Incomplete_Class Object

__PHP_Incomplete_Class means the class is unknown to PHP. Load the class before you start the session and everything should work



Related Topics



Leave a reply



Submit