In PHP 5.5, Finally may appear

Original author: Xinchen Hui
  • Transfer
Not so long ago, Nikita Popov, one of the activists of the “FOR PHP” movement, posted on his twitter message:

It very much looks like PHP 5.5 will have `finally` t.co/Dy93CZaR

(The original is here twitter.com/nikita_ppv/status/232930291625369600 )
Next, a free translation:

Representation


In this release of RFC, we will try to introduce you support for the new keyword “ finally ” in exceptions.

Without this feature, developers often have to write constructions of the form:
<?php
$db = mysqli_connect();
try {
   call_some_function($db);
} catch (Exception $e) {
   mysqli_close($db);
   throw $e;
}
mysql_close($db);


With the introduction of finally , we are not saying that you have to write less code, we are saying that you will get more control over try / catch constructs

Sentence


The finally block is always executed when the try / catch block
is completed. This means that the finally block is executed even if an exception is thrown. However, finally is actually more useful than simple exception handling. It allows programmers to avoid unnecessary constructions for cleaning memory and / or connections at the time of exception handling or if they were not caught.

<?php
$db = mysqli_connect();
try {
   call_some_function($db);// функция моджет выкинуть исключение, которое мы можем и не обработать
} finally {
   mysqli_close($db);

}

A touchy point when using the new construct is the use of the return construct in the try / catch block , in which case the finally block will still be called.

<?php
  try {
    return 2;
  } finally {
    echo "Этот текст мы увидим\n";
  }
  //Этот текст мы увидим
  echo "Этот текст мы НЕ увидим";

the result will be:
Well, in the case of nested try / catch / finally constructions :

<?php
function foo ($a) {
   try {
      echo "1";
      try {
        echo "2";
        throw new Exception("ex");
      } catch (Exception $e) {
        echo "3";
      } finally {
        echo "4";
        throw new Exception("ex");
      }
   } catch (Exception $e) {
      echo "3";
   } finally {
      echo "2";
   }
   return 1;
}
 
var_dump(foo("para"));

we get:

123432int(1)

Tests and Examples


There are also many examples and cases, you can peek further at
https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_001.phpt
https://github.com/laruence/php-src/ blob / finally / Zend / tests / catch_finally_002.phpt
https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_003.phpt
https://github.com/laruence/php-src/ blob / finally / Zend / tests / catch_finally_004.phpt
https://github.com/laruence/php-src/blob/finally/Zend/tests/catch_finally_005.phpt
https://github.com/laruence/php-src/ blob / finally / Zend / tests / catch_finally_006.phpt
https://github.com/laruence/php-src/blob/finally/Zend/tests/try_finally_001.phpt
https://github.com/laruence/php-src/blob/finally/Zend/tests/try_finally_002.phpt
https://github.com/laruence/php-src/blob/finally/Zend/tests/try_finally_003.phpt