My PHP object won’t serialize…wtf!?

December 31st, 2009 § 0

Everything serializes, right?  Not all the time, more information soon.

Basically, what I was trying to do was to serialize an object into SESSION and pull it later on.  Here’s what I did for a test:

  1. class test
  2. {
  3.     //some generic code here
  4. }
  5.  
  6. if (isset($_GET['id'])) {
  7.     var_dump($_SESSION);
  8.     var_dump(unserialize($_SESSION['test']));
  9. } else {
  10.     unset($_SESSION['test']);
  11.     $test = new test();
  12.     $_SESSION['test'] = $test;
  13.     header('Location: test.php?id=1');
  14.     exit();
  15. }

All this is is a quick hack that lets me both instantiate the class, set it to session, and look at another page to see if it is still set.  When I run this code, the class was being output just fine, meaning that it serialized just fine.  However, when I tried to serialize one of my classes (MyClass) in the same code, what I got after serializing was:

  1. N;

This is less than useful. 
I found on the php site, after quite a bit of frustrated searching, this post on the PHP.net site, that “N;” means basically “can’t serialize that”.

It turns out that my class had a call to __sleep() in it that was stubbed out and never actually implemented.  I removed this stubbed out method, which when I serialized my class gave me exactly what I was looking for.  I was able to get to any next script and access my object appropriately.

Tags: , , , , , , , , ,

Sphere: Related Content

Tagged: , , , , , , , , ,

§ Leave a Reply

What's this?

You are currently reading My PHP object won’t serialize…wtf!? at Reinforce Media, LLC.

meta

FireStats icon Powered by FireStats