If an object in PHP is stored in the session, $_SESSION, when will it’s __destruct method be called? I haven’t been able to find any clear answer in the manual. It does say that __destruct will be automatically called at the end of script execution, so my expectation is that it will be called regardless of it’s existence in $_SESSION.
Does this then mean that potentially __destruct will be called on every request for an object in the session?
Added:
Will the object be serialized before or after __destruct? Will modifications in __destruct be reflected in the next request?
You don’t actually store an object in session, you store it serialized as a string. You can then override the magic
__sleepand__wakeupmethods on the object.__sleepgets called when an object is serialized and__wakeupwill be called when you attempt to unserialize an object.For more info, see:
Manual Documentation for __sleep and __wakeup
Manual Documentation for Storing Serialized Objects in Session