3

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.

1

2 Answers 2

7

There's __sleep() and __wakeup().

Alternatively, you could implement Serializable.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for! Thanks, Ionut G. Stan!
0

Starting with PHP 7.4, there is also __serialize and __unserialize (see the Documentation) as a more usable alternative.

The Serializable interface might also be deprecated, and generally the magic methods are preferred (see also this).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.