PHP “__PHP_Incomplete_Class” error

May 18th, 2012 by Alex Leave a reply »

Had this error when loading an object from the $_SESSION?
Here’s why, from official PHP docs:

It’s possible to set a callback-function which will be called, if an undefined class should be instantiated during unserializing. (to prevent getting an incomplete object “__PHP_Incomplete_Class”.) Use your php.ini, ini_set() or .htaccess to define ‘unserialize_callback_func’. Everytime an undefined class should be instantiated, it’ll be called. To disable this feature just empty this setting.

A simple fix would be to load the session_start AFTER the object class declaration, but if it is not possible, then you could simply double-serialize the object. Like this:

// some code running in your application
// then,
$_SESSION['myobject'] = serialize($object);
// then when we need it
$object = unserialize($_SESSION['myobject']);

The reason of the error is because the objects stored in sessions are converted to serialized string, and when we call session_start(), it tries to initialize the object with the class definition which isn’t available yet.


Comments are closed.