Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61569 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64492 invoked from network); 20 Jul 2012 15:04:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jul 2012 15:04:16 -0000 X-Host-Fingerprint: 208.107.183.205 host-205-183-107-208.midco.net Date: Fri, 20 Jul 2012 11:04:15 -0400 Received: from [208.107.183.205] ([208.107.183.205:26035] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 64/87-18983-E6379005 for ; Fri, 20 Jul 2012 11:04:15 -0400 Message-ID: <64.87.18983.E6379005@pb1.pair.com> To: internals@lists.php.net References: <5007D707.2070100@hoa-project.net> User-Agent: slrn/pre1.0.0-18 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit X-Posted-By: 208.107.183.205 Subject: Re: Make try/catch brackets optinal From: weierophinney@php.net (Matthew Weier O'Phinney) On 2012-07-19, "Ivan Enderlin @ Hoa" wrote: > As you certainly know, brackets defining blocks in PHP are optional if > blocks contain a single instruction. Thus: > > if($condition) { > echo 'foobar'; > } > > is strictly equivalent to: > > if($condition) > echo 'foobar'; > > But this syntactic sugar is not applied uniformly to all PHP language > constructions. I have the try/catch couple in mind. > First, I would like to know why it is not possible to write: > > try > throw new Exception('foobar'); > catch(Exception $e) > var_dump($e->getMessage()); > > as a strict equivalence of: > > try { > throw new Exception('foobar'); > } > catch(Exception $e) { > var_dump($e->getMessage()); > } > > Second, if it is possible, could we plan to have this “feature” > (uniformity actually) in PHP6 (or maybe before)? I'd hesitate to call this a feature. If anything, for PHP6, I'd recommend the opposite: getting rid of brace-less blocks, period. I've read through the thread, and those proposing it talk about code readability, and use the argument "I've never had a problem before." To me, this means you've either (a) not been developing very long, and/or (b) not been working in long-lived projects, and/or (c) do not work with other people. Omitting braces makes understanding the intent of the code harder (did the author intend to only execute one statement, or did they forget the braces?), makes maintenance harder (a developer needing to add extra statements now also has to add the braces), and leads to hard-to-detect bugs (see previous two points). I've run into problems with brace-less blocks many, many times over the years. Clearly, enough other people have as well that any serious coding standard includes a clause requiring that all blocks use braces. I see no reason to add another context in which braces could be omitted. -- Matthew Weier O'Phinney Project Lead | matthew@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc