Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created December 8, 2021 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krakjoe/fe2daadbba671706f3da9a27b2f0d718 to your computer and use it in GitHub Desktop.
Save krakjoe/fe2daadbba671706f3da9a27b2f0d718 to your computer and use it in GitHub Desktop.
try-finally-auto
string(12) "Oh Exception"
Finally Foo::bar
Finally Foo::qux
Fatal error: Uncaught RuntimeException: Oh qux! in /opt/src/php-src/try-finally-auto.php:22
Stack trace:
#0 /opt/src/php-src/try-finally-auto.php(30): Foo->qux()
#1 {main}
thrown in /opt/src/php-src/try-finally-auto.php on line 22
diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
index a8bddfae50..246950a75c 100644
--- a/Zend/zend_language_parser.y
+++ b/Zend/zend_language_parser.y
@@ -981,7 +981,16 @@ absolute_trait_method_reference:
method_body:
';' /* abstract method */ { $$ = NULL; }
- | '{' inner_statement_list '}' { $$ = $2; }
+ | '{' inner_statement_list '}' catch_list finally_statement
+ {
+ zend_ast_list *catches = zend_ast_get_list($4);
+
+ if (catches->children == 0 && $5 == NULL) {
+ $$ = $2;
+ } else {
+ $$ = zend_ast_create(ZEND_AST_TRY, $2, $4, $5);
+ }
+ }
;
variable_modifiers:
<?php
function i_throw_oh_exception() {
throw new RuntimeException("Oh Exception");
}
class Foo {
public function bar() {
\i_throw_oh_exception();
} catch (\RuntimeException $ex) {
var_dump($ex->getMessage());
} finally {
echo "Finally " . __METHOD__ . PHP_EOL;
}
public function baz() {
echo "I am " . __METHOD__ . PHP_EOL;
} finally {
echo "Finally " . __METHOD__ . PHP_EOL;
}
public function qux() {
throw new \RuntimeException("Oh qux!");
} finally {
echo "Finally " . __METHOD__ . PHP_EOL;
}
}
$foo = new foo;
$foo->bar();
$foo->qux();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment