doumi1852 2016-09-03 08:21
浏览 11
已采纳

如何使Date-class在5.6.17和5.6.23上显示相同的结果

First of all I'm aware of this question: PHP Date bug. It was finally resolved on PHP 5.6.23. So as I'm moving some of my severs from PHP 5.6.17 to PHP 5.6.23 I want the proper behavior on all of servers. But the project is huge, so to change all the Date-class usages with version check is a bit overhead, as it must be a temporary approach.

So the questions are:

  • Is there a way to override Date-class on fly on symfony2 project with version check, so it could be the root point, which I could remove then?
  • Could I somehow log unexpected behavior (some general decision, as there're a lot of usages as I said).

  • If it's not possible, you are free to suggest something else.

  • 写回答

1条回答 默认 最新

  • dsjuimtq920056 2016-09-03 13:54
    关注

    As I see it you basically have three options. All have their drawbacks and advantages.

    Monkey patching the function / method using uopz:

    // first backup the original method
    uopz_backup (\DateTime::class, 'methodToOverwrite');
    
    // delete the original method
    uopz_delete(\DateTime::class, 'methodToOverwrite');
    
    // override the method
    uopz_function(\DateTime::class, 'methodToOverwrite', function($arg) {
        // do customn stuff like check for versions and handling them differently here 
        if (version_compare(PHP_VERSION, '5.6.23', '<')) {
            // fix stuff
            return fixedstuff;
        }
    
        return default stuff;
    });
    
    // once you are done with your code restore the original method
    uopz_restore (\DateTime::class, 'methodToOverwrite');
    

    Pros:

    • Don't have to change your code besides above
    • Centralized place for the code

    Cons:

    • High wtfness level because the method does something else then what people expect
    • You need to parse and properly handle whatever you are expecting in the method
    • Relies in a non default extension
    • Always have two paths to test in every instance

    Going in an fixing every instance manually:

    This would involve having version checks if statements all over your code.

    Pros:

    • It's clear what happens by just looking at the code

    Cons:

    • Having to find all occurrences
    • Having (possibly) a lot of (the same) version checks if statements littered throughout your code
    • People need to remember this

    Abstract away the issue into a dedicated function / class:

    It sounds like this thing that has the issue is something you are doing often. As such it would probably be better to abstract it away by putting it in a function / class.

    Pros:

    • Can easily be unit tested by just testing the function / class
    • It hides the complexity and the version if statements
    • Makes code's intent more clearer by having specific methods YourDate::getFirstTuesdayOfWeek()

    Cons:

    • Having to find all occurrences
    • People need to remember this

    Conclusion

    Imo the last option is the best because it keeps your code clean, easy to extent and easy to test. And on top of that it has the lowest level of wtfness.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大