Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51882 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91976 invoked from network); 14 Apr 2011 08:20:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2011 08:20:09 -0000 Authentication-Results: pb1.pair.com header.from=olemarkus@olemarkus.org; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=olemarkus@olemarkus.org; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain olemarkus.org from 213.236.166.183 cause and error) X-PHP-List-Original-Sender: olemarkus@olemarkus.org X-Host-Fingerprint: 213.236.166.183 unknown Received: from [213.236.166.183] ([213.236.166.183:49212] helo=remus.fluks.no) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F1/D7-44901-73EA6AD4 for ; Thu, 14 Apr 2011 04:20:09 -0400 Received: from localhost (localhost [127.0.0.1]) by remus.fluks.no (Postfix) with ESMTP id 6A7CE503629; Thu, 14 Apr 2011 10:20:04 +0200 (CEST) X-Virus-Scanned: amavisd-new at fluks.no Received: from remus.fluks.no ([127.0.0.1]) by localhost (remus.fluks.no [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VPBayfPqL4We; Thu, 14 Apr 2011 10:20:02 +0200 (CEST) Received: from localhost (trh.betradar.com [92.62.38.2]) by remus.fluks.no (Postfix) with ESMTPA id 854F0503609; Thu, 14 Apr 2011 10:20:02 +0200 (CEST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "internals@lists.php.net" , "Ben Schmidt" References: <4D950434.3060704@yahoo.com.au> <4D9E0543.1080600@lerdorf.com> <69.82.36433.EC33E9D4@pb1.pair.com> <4D9E34C4.5000406@lerdorf.com> <4D9E429B.20503@sugarcrm.com> <4D9E96B6.6060401@lerdorf.com> <718216446.20110408143441@cypressintegrated.com> <4DA0E71C.9030008@gmail.com> <4DA63ED8.4080402@yahoo.com.au> Date: Thu, 14 Apr 2011 10:20:01 +0200 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <4DA63ED8.4080402@yahoo.com.au> User-Agent: Opera Mail/11.10 (Linux) Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: olemarkus@olemarkus.org ("Ole Markus With") On Thu, 14 Apr 2011 02:24:56 +0200, Ben Schmidt wrote: >> I think these shortcuts could be really useful for array elements, but >> for other variables I am really sceptical and I think they would do >> more harm than good. Generally I do not really see any reason why a >> variable should not be 'instanciated' before use. >> >> So >> +1 if this shortcut is implemented to only work for array elements. >> -1 for any other variable type. > > There are two issues here. > > 1. Suppression of notice. I agree, it is best done only for array > keys. It's not hard to initialise a variable with $var=null at the > beginning of a code block to avoid such a notice, and that is the > appropriate way to do it for variables. > > 2. Offering a shortcut for the common idiom isset($x) ? $x : $y in > line with the DRY design principle. A notice would never be emitted > here in any case. The problem is that this idiom is still in wide use > despite the shortcut ternary operator already provided, because an > isset() check is different to a boolean cast. > > Some thoughts: > > - The actual intent of 2. is probably $x!==null ? $x : $y i.e. it's > not about suppressing notices at all, but about offering a default > value, and the idiom quite probably only uses isset() because it > predated null in the language. > I have never felt the need for a shortcut in these cases. It would be interesting to see some code where this would be practical. > - If we view 2. in this way, the two problems are independent, and it > seems to me it would be best to solve them independently, rather > than with a single operator. > > So, I suggest: > > 1. An array lookup mechanism that suppresses the notice for undefined > keys. It would work the same as regular array index lookups except > that the notice for undefined keys (and only for undefined keys) > would not be generated (it would not just be hidden, but would never > be even generated). This is what I feel PHP is missing. Particularly when it comes to multi-dimensional arrays. Because this feature is missing I tend to do something like function generateHash($x, $y, $z) { return "$x::$y::$z"; } if (isset($a[generateHash($x, $y, $z)]) { ... } instead of if (isset($a[$x]) && isset($a[$x][$y]) && isset($a[$x][$y][$z])) { ... } > Arguing about syntax is then a second step. However, my views on this > are: > I think it best to avoid discussing the actual syntax before agreeing on what we really need. -- Ole Markus With Systems Architect - Sportradar AS Developer - Gentoo Linux