Friday, January 01, 2010

serialize/unserialize php API - an observation

On first request, I was saving the object into session.
$userInstance = new Users_Model();
$_SESSION['__key'] = $userInstance;
Make sure the class Users_Model definition is available.

On second request, I retrieve the object from session for re-use.
$retrievedUserInstance = $_SESSION['__key'];
public '__PHP_Incomplete_Class_Name' => string 'Users_Model' (length=11)
private 'id' => string '1' (length=1)
// ...
I noticed the $retrieveUserInstance object missed several methods!!

I realized that on second request, the class Users_Model definition
is not available on the page load and unserialize API is not working as expected.

$_SESSION values are saved using serialize API (as session.save_handler was set to files)

On making the class definition available before session_start() call,
the $retrieveUserInstance worked fine.
private 'id' => string '1' (length=1)
// ...
Hope it saves sometime for you.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?