douweibeng5219 2011-07-05 23:30
浏览 49
已采纳

日期和货币是否应该作为对象传递?

Help me settle an argument with a coworker...

We’re using Zend Framework and PHP and much of the data we deal with includes dates and prices. I’ve been arguing that when the data is retrieved (either from the database or JSON) we should automatically immediately convert the date properties to Zend_Date objects and the price properties to Zend_Currency objects so as to ensure that we are always using the abilities of those classes when manipulating/comparing/displaying the data. My argument is that it ensures consistency and we always know what form the data is in.

My coworker argues that we should just leave the data as is (strings) when retrieved and only create the Zend_Date/Zend_Currency objects when used in the model or view.

Who is right and why?

(I’m sure someone will argue that there is a small speed optimization penalty in creating the objects, but in our case that is not of concern.)

  • 写回答

1条回答 默认 最新

  • dougou3871 2011-07-05 23:45
    关注

    In OO the best practice would surely be to use objects as they encapsulate the state and behaviour of what you expect a currency/date to be.

    I am not experienced with Zend but I would assume the date class would do some checking to ensure that the date is a valid date or throw an exception. If you wait until the last minute to convert the date to an object you are potentially making it harder to find the source of the bug and transporting around tainted data the whole time. My defensive nature would want me to sanitize data as soon as possible that has not come from a known trusted source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog