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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题