duannian3494 2015-12-29 20:12
浏览 58
已采纳

php代码逻辑版本

I want a solution something like: Imagine I have try catch block, it works in any php 5.?.? version but with finally block not in any. my code must be like this:

try {

    // some logic

} catch (Exception $ex) {
    // some logic
} finally {
    // other logic
}

but, how can I make so that finally block worked only if php version supports it else ignore? For example:

try {
    // some logic
} catch (Exception $ex) {
    // some logic
}
#if version=5.?.?
finally {
    // other logic
}
#endif

Is there any solution?

  • 写回答

3条回答 默认 最新

  • dpwfh874876 2015-12-29 20:21
    关注

    You can get php version by phpversion method. The only option you have is to write an if statement to check it; if it supports write the whole try-catch-finally block. If it does not, write only try-catch. You can use inline functions to prevent duplicate code. I have no idea why somebody needs this, but for example;

    function fncTry(){...}
    function fncCatch($exception){...}
    function fncFinally(){...}
    
    if($phpVersion == '5.5'){
       try { fncTry(); }
       catch(Exception $ex) { fncCatch($ex); }
       finally { fncFinally(); }
    }else{
       try { fncTry(); }
       catch(Exception $ex) { fncCatch($ex); }
    
       fncFinally();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集