|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
Patchesphp-5.4.3-alt-try-catch.diff (last revision 2012-05-26 07:00 UTC by kriss at krizalys dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
[2012-05-29 03:17 UTC] kriss at krizalys dot com
[2015-03-21 20:36 UTC] nikic@php.net
-Status: Open
+Status: Wont fix
[2015-03-21 20:36 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 14:00:01 2025 UTC |
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