Edit report at https://bugs.php.net/bug.php?id=62164&edit=1
ID: 62164 Comment by: kriss at krizalys dot com Reported by: kriss at krizalys dot com Summary: [PATCH] Add support for alternative try/catch syntax Status: Open Type: Feature/Change Request Package: Scripting Engine problem Operating System: * PHP Version: 5.4.3 Block user comment: N Private report: N New Comment: I tested the patch successfully on Gentoo x86_64/PHP-5.4.3 Previous Comments: ------------------------------------------------------------------------ [2012-05-26 06:59:33] kriss at krizalys dot com Description: ------------ The attached patch adds support to alternatively open a try block using a colon (:) in try/catch structures. When using this syntax, each catch block must also be open by a colon, try and catch blocks must not be closed by a closing brace (}), and the whole try/catch structure must be closed by a "endtry" keyword. The idea is to provide greater readability when PHP code is intermixed with HTML code and keep consistency with constructs like if/endif, for/foreach, etc... Please, note that after patching, the Zend scanner and parser, as well as ext/tokenizer_data.c should be regenerated (using re2c, bison and tokenizer_data_gen.sh respectively, or using the provided Makefile targets). Test script: --------------- <?php try: echo "Trying something unsafe\n"; throw new Exception('thrown using alternative try/catch syntax'); catch (int $e): echo "Caught exception of type int (ints are not throwable, this is just example)\n"; // several statements can also be used here catch (Exception $e): echo "Caught exception of type Exception: {$e->getMessage()}\n"; // several statements can also be used here endtry; ?> <?php try:?> <p>Trying something unsafe</p> <?php throw new Exception('thrown using alternative try/catch syntax');?> <?php catch (int $e):?> <p>Caught exception of type int (ints are not throwable, this is just example)</p> <?php catch (Exception $e):?> <p>Caught exception of type Exception: <?php echo $e->getMessage();?></p> <?php endtry;?> Expected result: ---------------- Trying something unsafe Caught exception of type Exception: thrown using alternative try/catch syntax <p>Trying something unsafe</p> <p>Caught exception of type Exception: thrown using alternative try/catch syntax</p> Actual result: -------------- PHP Parse error: syntax error, unexpected ':', expecting '{' in /home/aaa/test.php on line 2 Parse error: syntax error, unexpected ':', expecting '{' in /home/aaa/test.php on line 2 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62164&edit=1