Req #62164 [Opn->Wfx]: [PATCH] Add support for alternative try/catch syntax

From: Date: Sat, 21 Mar 2015 20:36:31 +0000
Subject: Req #62164 [Opn->Wfx]: [PATCH] Add support for alternative try/catch syntax
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191512@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62164&edit=1

 ID:                 62164
 Updated by:         nikic@php.net
 Reported by:        kriss at krizalys dot com
 Summary:            [PATCH] Add support for alternative try/catch syntax
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        5.4.3
 Block user comment: N
 Private report:     N

 New Comment:

Closing as Won't Fix, as I don't think PHP is interested in supporting alternative syntax
for more control structures. If you really think this feature is necessary, please submit an RFC (or
at least a discussion on the PHP internals mailing list).


Previous Comments:
------------------------------------------------------------------------
[2012-05-29 03:17:36] kriss at krizalys dot com

I tested the patch successfully on Gentoo x86_64/PHP-5.4.3

------------------------------------------------------------------------
[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


Thread (3 messages)

« previous php.bugs (#191512) next »