PiTheNumber
PiTheNumber

Reputation: 23542

When do I need serialize objects to store in a session?

I know there is serialize()/unserialize() to store objects with magic __sleep and __wakeup methods. But I have a project where objects are stored in $_SESSION without any problems.

Now I am wondering when I need to use serialize?

Is it for cross references?

Upvotes: 1

Views: 551

Answers (1)

Jon
Jon

Reputation: 437434

PHP internally uses the equivalent of serialize/unserialize when you put objects inside $_SESSION, so as long as you obey the rules (don't try to serialize resources, provide class definitions for unserializing) it all works automatically as you have already seen.

You would need to explicitly use serialize/unserialize only if you intend to persist values using some custom mechanism.

Upvotes: 6

Related Questions