Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:74264 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33418 invoked from network); 16 May 2014 15:09:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 May 2014 15:09:03 -0000 Authentication-Results: pb1.pair.com header.from=are.you.winning@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=are.you.winning@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) X-PHP-List-Original-Sender: are.you.winning@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qc0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:58570] helo=mail-qc0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C5/AB-58167-E0A26735 for ; Fri, 16 May 2014 11:09:02 -0400 Received: by mail-qc0-f170.google.com with SMTP id i8so4604559qcq.1 for ; Fri, 16 May 2014 08:08:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:content-type; bh=++QAGFja9p0e97o+yVeKmnkNDkJKxg2etEM1lisTgoE=; b=HDtKfFahMBGigrSlPknu9sODTclZ5WQVlowF2nSeNLRBIva+lEq8wju0k6PuMCdMBg DbWjc+d6YPuX6cUBOcFv2WdzVxpno3j2S/wdmquy4WOV8Wa09Bg+vScMjnn7qRRFnJJ6 pRz6xD8BmVwALOtS7itQ2o68O6O5Rtxa4xjc/BA5HRHnKdRJ/v+ChzcJ4OIJHuqIURSG MDi9b152V1T7c/T8oC4SSIQY5+p70aHPO2tjAzOfsqs1Py2C/fW2O8EAloOhM2TzwASb Z+XmBKrSzwvkL5eghfSzUatuScbe/PQhf/AS4uxTum5dPs7zWF1V/yyEAFP+rLSasBvw UXJw== MIME-Version: 1.0 X-Received: by 10.224.97.69 with SMTP id k5mr24070105qan.8.1400252939658; Fri, 16 May 2014 08:08:59 -0700 (PDT) Sender: are.you.winning@gmail.com Received: by 10.229.148.145 with HTTP; Fri, 16 May 2014 08:08:59 -0700 (PDT) In-Reply-To: <20140516144408.GW14095@phcomp.co.uk> References: <20140516144408.GW14095@phcomp.co.uk> Date: Fri, 16 May 2014 16:08:59 +0100 X-Google-Sender-Auth: 1ZuYGaL-COkdsFuEH14RgdB3UkY Message-ID: To: internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Cannot use const within try block From: daverandom@php.net (Chris Wright) On 16 May 2014 15:44, Alain Williams wrote: > The code below gives me: PHP Parse error: syntax error, unexpected T_CONST in ... > > try { > const XXX = 12; > > echo "XXX = " . XXX . "\n"; > > }catch (ErrorException $e) { > echo "error {$e->getMessage()}\n"; > } > > It works if I put the 'cost' before/outside the 'try'. > > It also works if I replace the const line by: > > define('XXX', 'twelve'); > > or > > include "testdef.php"; > > where testdef.php contains: > > const XXX = 12; > Defining constants using const is handled at compile time. Because of this, it cannot be dependent on branches that are only processed at run time. When you include a file, this creates a new "compile-time" for the included file, so the operation succeeds. define() is a regular function call evaluated at run time. As such if you want a constant defined conditionally then you'll need to use define() > I cannot find this behaviour documented. > > a) If there is no good reason for this restriction - can it be removed. > > or > > b) If there is good reason for it can this be documented. This behaviour is documented; to quote the note at http://php.net/const "As opposed to defining constants using define(), constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops or if statements." I will update the note shortly to include try/catch blocks. Thanks, Chris