PHP serialization with non-serializable parts

PHPserialization

I have a PHP class that stores database query results, but also contains a PDO object so that the results can be updated/re-inserted/etc on the fly. A makeshift ORM, if you will.

Problem is, I need to serialize this class but instances of PDO cannot be serialized. I'm ok with that; by the time the object gets serialized, I have no need for the PDO instance.

Is there a way to mark a variable for exclusion from serialization within a class, like there are with some other languages? I understand I could manually unset() the PDO variable before I want to serialize the class, but with the current structure of the code, that would be a bit of a nightmare.

My saving grace here would be a __serialize() method that could be overridden, but it doesn't appear anything like that exists.

Best Answer

There's __sleep() and __wakeup().

Alternatively, you could implement Serializable.