Skip to content

Commit 80d5ae3

Browse files
committed
Implemented 'finally' keywords for php
RFC: https://wiki.php.net/rfc/finally FR: https://bugs.php.net/bug.php?id=32100 and I have got some improvment ideas(performance), will implemented later. thanks
1 parent e51acee commit 80d5ae3

25 files changed

Lines changed: 1830 additions & 686 deletions

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PHP NEWS
33
?? ??? 201?, PHP 5.5.0
44

55
- General improvements:
6+
. Implemented 'finally' keyword (https://wiki.php.net/rfc/finally). (Laruence)
67
. Drop Windows XP and 2003 support. (Pierre)
78
. World domination
89
. Improve set_exception_handler while doing reset.(Laruence)

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ PHP X.Y UPGRADE NOTES
3131
2. New Features
3232
========================================
3333

34+
- Support finally keyword. (Laruence)
35+
(wiki.php.net/rfc/finally)
3436
- Support constant array/string dereferencing. (Laruence)
3537
(https://wiki.php.net/rfc/constdereference)
3638
- Add support for using empty() on the result of function calls and

Zend/tests/catch_finally_001.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Try catch finally
3+
--FILE--
4+
<?php
5+
function foo ($throw = FALSE) {
6+
try {
7+
echo "try\n";
8+
if ($throw) {
9+
throw new Exception("ex");
10+
}
11+
} catch (Exception $e) {
12+
echo "catch\n";
13+
} finally {
14+
echo "finally\n";
15+
}
16+
17+
echo "end\n";
18+
}
19+
20+
foo();
21+
echo "\n";
22+
foo(true);
23+
?>
24+
--EXPECTF--
25+
try
26+
finally
27+
end
28+
29+
try
30+
catch
31+
finally
32+
end

Zend/tests/catch_finally_002.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Try catch finally return
3+
--FILE--
4+
<?php
5+
function foo () {
6+
try {
7+
echo "try\n";
8+
return 1;
9+
} catch (Exception $e) {
10+
} finally {
11+
echo "finally\n";
12+
}
13+
return 2;
14+
}
15+
16+
var_dump(foo());
17+
?>
18+
--EXPECTF--
19+
try
20+
finally
21+
int(1)

Zend/tests/catch_finally_003.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Try catch finally multi-return
3+
--FILE--
4+
<?php
5+
function dummy($msg) {
6+
var_dump($msg);
7+
}
8+
9+
function foo ($a) {
10+
try {
11+
dummy("try");
12+
return $a;
13+
} catch (Exception $e) {
14+
throw $e;
15+
} finally {
16+
dummy("finally");
17+
return "finally";
18+
}
19+
return "end";
20+
}
21+
22+
function &bar($a) {
23+
try {
24+
echo "try\n";
25+
throw new Exception("ex");
26+
} catch (Exception $e) {
27+
} finally {
28+
return $a;
29+
}
30+
return ($c = "end");
31+
}
32+
var_dump(foo("para"));
33+
var_dump(bar("para"));
34+
?>
35+
--EXPECTF--
36+
string(3) "try"
37+
string(7) "finally"
38+
string(7) "finally"
39+
try
40+
string(4) "para"

Zend/tests/catch_finally_004.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Nesting try catch finally
3+
--FILE--
4+
<?php
5+
6+
function throw_exception($msg) {
7+
throw new Exception($msg);
8+
}
9+
10+
function foo (&$ex) {
11+
try {
12+
echo "1";
13+
try {
14+
echo "2";
15+
throw_exception("try");
16+
} catch (Exception $e) {
17+
echo "3";
18+
throw_exception("catch");
19+
} finally {
20+
echo "4";
21+
throw_exception("finally");
22+
}
23+
} catch (Exception $e) {
24+
$ex = $e;
25+
echo "3";
26+
} finally {
27+
echo "2";
28+
}
29+
return 1;
30+
}
31+
32+
var_dump(foo($ex));
33+
34+
do {
35+
var_dump($ex->getMessage());
36+
} while ($ex = $ex->getPrevious());
37+
?>
38+
--EXPECT--
39+
123432int(1)
40+
string(7) "finally"
41+
string(5) "catch"

Zend/tests/catch_finally_005.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Try catch finally with return
3+
--FILE--
4+
<?php
5+
function foo ($a) {
6+
try {
7+
throw new Exception("ex");
8+
} catch (PdoException $e) {
9+
die("error");
10+
} catch (Exception $e) {
11+
return 2;
12+
} finally {
13+
return 3;
14+
}
15+
return 1;
16+
}
17+
18+
var_dump(foo("para"));
19+
?>
20+
--EXPECTF--
21+
int(3)

Zend/tests/catch_finally_006.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Try catch finally: re-throw exception in catch block
3+
--FILE--
4+
<?php
5+
function foo ($a) {
6+
try {
7+
throw new Exception("ex");
8+
} catch (Exception $e) {
9+
var_dump($a);
10+
throw $e;
11+
} finally {
12+
var_dump("finally");
13+
return "return";
14+
}
15+
return 1;
16+
}
17+
18+
try {
19+
var_dump(foo("para"));
20+
} catch (Exception $e) {
21+
"caught exception" . PHP_EOL;
22+
var_dump($e->getMessage());
23+
}
24+
?>
25+
--EXPECT--
26+
string(4) "para"
27+
string(7) "finally"
28+
string(2) "ex"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Try catch finally
3+
--FILE--
4+
<?php
5+
6+
class AE extends Exception {};
7+
class BE extends Exception {};
8+
9+
function foo () {
10+
try {
11+
try {
12+
try {
13+
throw new Exception("try");
14+
} catch (AE $e) {
15+
echo "0";
16+
die("error");
17+
} finally {
18+
echo "1";
19+
}
20+
} finally {
21+
echo "2";
22+
}
23+
} catch (BE $e) {
24+
die("error");
25+
} catch (Exception $e) {
26+
echo "3";
27+
} finally {
28+
echo "4";
29+
}
30+
return 1;
31+
}
32+
33+
var_dump(foo());
34+
?>
35+
--EXPECTF--
36+
1234int(1)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Try catch finally
3+
--FILE--
4+
<?php
5+
6+
class AE extends Exception {};
7+
class BE extends Exception {};
8+
9+
function foo () {
10+
try {
11+
try {
12+
try {
13+
try {
14+
echo "1";
15+
throw new Exception("try");
16+
} catch (AE $e) {
17+
die("error");
18+
} finally {
19+
echo "2";
20+
}
21+
} finally {
22+
echo "3";
23+
}
24+
} catch (BE $e) {
25+
die("error");
26+
} finally {
27+
echo "4";
28+
}
29+
} catch (Exception $e) {
30+
echo "5";
31+
} catch (AE $e) {
32+
die("error");
33+
} finally {
34+
echo "6";
35+
}
36+
return 7;
37+
}
38+
39+
var_dump(foo());
40+
?>
41+
--EXPECTF--
42+
123456int(7)

0 commit comments

Comments
 (0)