0

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?

Arkadi
  • 1,153
  • 1
  • 14
  • 35
  • 3
    If you can make without finally block -- why use finally block? – Deep Dec 29 '15 at 20:17
  • if I will throw exception in catch block there always will be executed code inside `finally` nevertheless of `throw` – Arkadi Dec 29 '15 at 20:34
  • [This answer](http://stackoverflow.com/a/24427168/4577762) could be related: _You could include different scripts based on version_ – FirstOne Dec 29 '15 at 20:43

3 Answers3

2

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();
}
abeyaz
  • 3,034
  • 1
  • 16
  • 20
1

Yes.

phpversion() will return a string of the php version. (Example: "5.6.14")

You can use that to set up your conditional statement.

P. Gearman
  • 1,138
  • 11
  • 14
0

i am sorry i'm confused :)

check this

and you could check about phpversion before you make the try catch

if (php version == 5.5 or greater)

you make it with finally

else you make it without finally

Community
  • 1
  • 1
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30