更新__destruct()上的数据库? [英] Updating database on __destruct()?
问题描述
您认为这是个好主意吗?
Do you think it's a good idea?
假设您有一个应用程序组件,其他组件可以使用该组件来检索/更新数据库中的数据。基本上,这是一个具有get(),set(),update()方法的类。
Let's say you have an application component that is used by other components to retrieve / update data in the db. It's basically a class with get(), set(), update() methods.
该组件仅更新(或设置)数据是一个好主意吗?在它的属性被调用时,以及在__destruct上也更新数据库?还是应该在每次set / update调用时直接更新数据库?
Would it be a good idea for that component to update (or set) data only in it's properties when called, and on __destruct to update the db as well? Or should it directly update the db on each set/ update call ?
推荐答案
在对象破坏方面更新数据库对我来说有点气味有点像软件 副作用 。就是说,在意外而又不太明显的地方发生的动作。从代码中查看并不会明显看出,调用 __ destruct()
时会发生数据库操作,即使您显式调用它也是如此。试图查找涉及数据不一致的错误时,将来的代码维护者(包括您自己)很容易混淆,但是在查看代码时看不到对数据库的任何调用或类似于数据交互的方法调用。
Updating the database on object destruction smells to me a little bit like a software side effect. That is, an action that takes place in an unexpected and somewhat non-explicit place. It would not be obvious from viewing your code that a database action is happening when __destruct()
is called, even if you call it explicitly. Future code maintainers (including yourself) could be easily confused when trying to hunt down a bug involving inconsistent data, but not seeing any calls to the database or method calls resembling data interactions when viewing the code.
我建议不要这样做。
这篇关于更新__destruct()上的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!