views:

47

answers:

1

I usually use a classes destructor method __destruct() to persist objects to session or what have you. It is just very convinient, but I'm curious to if there are any other methods that are equally appealing. Do you know of such?

The curiousity arose as I was to merge/utilize two frameworks that both made use of __destruct() for persistance resulting in a race-problem.

A: 

You should handle the persistence manually and explicitly. Not only is it more intuitive for anyone who has to read your code, but it's not a good idea to depend on PHP's garbage collector to do important jobs for you, since it can be unpredictable. Your objects may not be saved in the right order, either, if that matters.

It also prevents many debugging nightmares.

Lotus Notes
What is your opinion if the objects to persist are more temporal, like the state of an application, a shopping basket for instance. To keep state across requests by serialicing the basket to the session, and deserialicing it upon each new request. Again __destruct() seems like a very good place to do this. I agree with you that model objects like for instance products, user objects, etc. should be handled manually and explicitly. Humor me about the state data example, would that in ur opinion be an ok use of __destrcut() or what would you do?
Michael