Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70430 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5361 invoked from network); 26 Nov 2013 21:52:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Nov 2013 21:52:02 -0000 Authentication-Results: pb1.pair.com smtp.mail=jezzgoodwin@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jezzgoodwin@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.52 as permitted sender) X-PHP-List-Original-Sender: jezzgoodwin@gmail.com X-Host-Fingerprint: 209.85.219.52 mail-oa0-f52.google.com Received: from [209.85.219.52] ([209.85.219.52:33834] helo=mail-oa0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 98/3D-39355-00815925 for ; Tue, 26 Nov 2013 16:52:00 -0500 Received: by mail-oa0-f52.google.com with SMTP id h16so6754835oag.11 for ; Tue, 26 Nov 2013 13:51:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=umjggBeT4D47TtNsXyTYuh1ojLi2D6XUBsw4F94YPm4=; b=vMYBF/HTo5inlIdhYsXwuTpwBa0fZliKrqIDZ4ZQZ0e/AA2CTif7ibCl2tELXCMVFr 38KA3uYRS9DREQZQ+IaYKszno25wUE+1PLo/SEv/IqViBF4yMA9ly9VCCnC//+Y0iVyU aP3x7zZ/u2FhOy3RrJ/cy2GTj9OzinSkav0OdEyEcge5z3O4KLI9A9i2o5e7hwbbJ7L0 O9g9jLMhrkuTRofAj3H7w2/snOA8ghYzT3aW/mZsWOoNG5uDEEuicyVj16pONQHm3nRI I2wxZwtMTfELVilqLFZqpuw5IafZEUfBnDCqOZGMaw0mDk+4jtsAAEnflg+LpmJ/W2pr 99jg== MIME-Version: 1.0 X-Received: by 10.182.66.164 with SMTP id g4mr8053152obt.47.1385502717338; Tue, 26 Nov 2013 13:51:57 -0800 (PST) Received: by 10.182.167.5 with HTTP; Tue, 26 Nov 2013 13:51:57 -0800 (PST) In-Reply-To: References: Date: Tue, 26 Nov 2013 21:51:57 +0000 Message-ID: Cc: PHP internals Content-Type: multipart/alternative; boundary=089e0160c35e4d6c1b04ec1b7df8 Subject: Re: [PHP-DEV] [Proposal] Modification to ?: functionality From: jezzgoodwin@gmail.com (Jezz Goodwin) --089e0160c35e4d6c1b04ec1b7df8 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Tue, Nov 26, 2013 at 7:46 PM, Peter Cowburn wrot= e: > > > > On 26 November 2013 18:35, Jezz Goodwin wrote: > >> Hi, >> >> Before mentioning anything about new syntax, I thought I'd point out tha= t >> this code is already possible for setting defaults: >> >> (isset($foo)) ?: $foo =3D 'default'; >> >> or >> >> isset($foo) ?: $foo =3D 'default'; >> >> (I personally prefer wrapping the condition within brackets) >> >> Saying that, I do think a syntax for setting defaults which doesn't >> involve >> typing the variable name twice would be nice. >> >> My favourite one listed so far is: >> >> $foo ?=3D 'default'; >> > > I like that too. > > Elaborating on that idea, here=92s one idea from a while back, making muc= h > wider use of a =93?=94 operator=85 https://gist.github.com/salathe/325682= 6 > > I worry sometimes that new syntax features are suggested for cleverness sake and not for making code 'better'. My definition of better code is code that is easy to read, concise, easy to maintain and ideally has good performance. Personally I think this feature can be used to make better code. For example, I use a utility function to make writing default variable values cleaner: function fallback ( &$var, $default=3Dnull ) { if (!isset($var)) { $var =3D $default; } } So, within other functions I use code like this: fallback($name, 'no name'); The main thing that bugs me about using this code is that it has to invoke a function to do it's work. If you've got lots of default values, it's a lot of function calls. This new syntax would lower the number of function calls, and be just as concise. Footnote: Although not quite the same subject, another way to increase performance with this type of code would be a way to tell the parser to inline functions. I'd love to be able to write 'inline function fallback ...' and be able to give my code better grammar without the performance penalty. --089e0160c35e4d6c1b04ec1b7df8--