Database update on __destruct ()?

Do you think this is a good idea?

Let's say you have an application component that is used by other components to retrieve / update data in db. This is basically a class with get (), set (), update () methods.

It would be nice if this component updated (or installed) the data only in the properties when called, and on __destruct - update db? Or should it update db directly with every call to set / update?

+5
source share
2 answers

Updating the object destruction database smells a bit to me as a side effect. That is, an action that occurs in an unexpected and somewhat implicit place. From looking at your code, it would not be obvious that the database action occurs on a call __destruct(), even if you call it explicitly. Future code developers (including yourself) can easily be confused by trying to track down an error related to inconsistent data, but not seeing calls to database or method calls that are similar to data interactions when viewing code.

I would advise doing this.

+5
source

Attempting to throw an exception from the destructor (called at the time the script completes) causes a fatal error.

, ? , , , , .

+2

All Articles