Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51896 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38214 invoked from network); 14 Apr 2011 13:12:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2011 13:12:28 -0000 Authentication-Results: pb1.pair.com header.from=mail_ben_schmidt@yahoo.com.au; sender-id=unknown; domainkeys=good Authentication-Results: pb1.pair.com smtp.mail=mail_ben_schmidt@yahoo.com.au; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain yahoo.com.au from 98.139.52.248 cause and error) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: mail_ben_schmidt@yahoo.com.au X-Host-Fingerprint: 98.139.52.248 nm29-vm0.bullet.mail.ac4.yahoo.com Received: from [98.139.52.248] ([98.139.52.248:48300] helo=nm29-vm0.bullet.mail.ac4.yahoo.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E8/D1-25347-BB2F6AD4 for ; Thu, 14 Apr 2011 09:12:28 -0400 Received: from [98.139.52.191] by nm29.bullet.mail.ac4.yahoo.com with NNFMP; 14 Apr 2011 13:12:24 -0000 Received: from [98.138.90.56] by tm4.bullet.mail.ac4.yahoo.com with NNFMP; 14 Apr 2011 13:12:24 -0000 Received: from [98.138.88.234] by tm9.bullet.mail.ne1.yahoo.com with NNFMP; 14 Apr 2011 13:12:24 -0000 Received: from [127.0.0.1] by omp1034.mail.ne1.yahoo.com with NNFMP; 14 Apr 2011 13:12:24 -0000 X-Yahoo-Newman-Id: 451862.32751.bm@omp1034.mail.ne1.yahoo.com Received: (qmail 71326 invoked from network); 14 Apr 2011 13:12:24 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.au; h=DKIM-Signature:Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=lodeLT2S0P1cRIrgrlHCWOeRJZttHCsUBil2QiiU92NsMIcxhEzx6qU1AXFj1dek/nCHKrYXiC0LsiVip2pVn1IvmGupcrHbMHREZNNHVmK4EDKkzj/OHPiDE5CQAhZZj6wHWzJXIep8/bZaGXclaP6zmtcqPjWJK6ydHTZpdc0= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.au; s=s1024; t=1302786744; bh=VT15ggPg9xafLa6YU1V6PiSA7CLf7f2bm+Ia02vrBH4=; h=Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:References:In-Reply-To:Content-Type:Content-Transfer-Encoding; b=uIZAxTaO892pkg3dFRT6RLoSzmtB1npua/mrjya8LfNHRNlUk4U0dPbvPSfupRCmSpgoW6gEkA3IgylMjl/hchLmmmBmVHQNNr0+Ts//ddnLdhnRfXA9NfdC3ndYYf6BnA3aBbvyEEOTdXEdAJ2tj3kt+6PmWld1nn2GUMQ7BUw= Received: from thought.local (mail_ben_schmidt@124.168.69.175 with plain) by smtp131.mail.mud.yahoo.com with SMTP; 14 Apr 2011 06:12:23 -0700 PDT X-Yahoo-SMTP: enFMnPSswBAexaHyzgobwuUTrYOhZdJ0KRA2SjA- X-YMail-OSG: zvtOFLUVM1lwwJ1plwnR5eDMme3NENgo2HTGLJ0hPN7YuAi PSICEwBlpRPbFfShsVMhksHoKL_wqI9JTrt6p98L3RtYmfKAXKsuWd66Gwwk 9iz4gaU7eWM5drmJ52kvZmqtgU1Xfs_kRZrUwYcnefVlLmOT4hJRvD_UspUc Q5G_YaRqizHf.CGHAcJMjR5vuMFFrsYv6w2cz40YTX9YND.VwexqHefCW9ZI HHsqmb1WCH8j7jHgPHwE32s5KFqAfwQNiEQ1xbo2jrmBrJm2DMCv347uS6LT J9LWF9.PGsluWP5e6cR_Xm2HgzHXl7NxpnpSP.UKHCPLIkRatPsEyTnLkzho tlDMmHY2DJbuQ3FmR0hGI X-Yahoo-Newman-Property: ymail-3 Message-ID: <4DA6F2BC.10706@yahoo.com.au> Date: Thu, 14 Apr 2011 23:12:28 +1000 User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 ThunderBrowse/3.3.5 MIME-Version: 1.0 To: RQuadling@googlemail.com CC: Richard Quadling , Eloy Bote Falcon , Ole Markus With , "internals@lists.php.net" 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> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Implicit isset/isempty check on short-ternary operator From: mail_ben_schmidt@yahoo.com.au (Ben Schmidt) >>>> 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. >>>> >>>> - 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). >>>> > http://news.php.net/php.internals/51877 > > array_key_exists($key, $array) for arrays > array_key_exists($varname, get_defined_vars()) for locally scoped variables. Apart from being long and ugly, surely that is horribly inefficient. > No need to use @. True. And I don't think anybody is. We all know @ is dangerous and nasty and don't use it. We're not seeking an alternative to @, we're seeking an alternative to repeating ourselves by using isset()/array_key_exists()/is_null() as well as the value being tested. But we don't want to do this in a blanket way similar to @ where a whole bunch of notices are suppressed. We want to specify precisely where missing values are allowable by indicating exactly which array index lookups may silently fail (and evaluate to null). Basically we don't want to make again the mistake that @ was. > Are they attempting to determine the existence of a variable/index > entry or are they attempting to determine if the variable/element is > null. For me, existence and nullness are basically the same, and I think this is the common case. The whole point of being able to set something to null is to have a 'value' to represent 'unsetness'. This is why I think solving the conditional problem should use a !==null test. That gives the flexibility to use/pass null to represent 'unsetness' but doesn't pick up zero, false, etc. like a boolean cast does. Using array_key_exists() would remove that flexibility and be less useful. As far as silencing notices goes, the rationale is that basically we want to flag that 'null is OK, even if it's a fallback'. I.e. we don't care whether a value is null because it was set to null, or because null is a fallback because the variable was never defined. Either way, null is OK, so don't tell me about it. The conditional side lets us handle nulls nicely by providing our own defaults/fallbacks if it appears. The notice-suppression side lets us say that null is OK, even if that null itself is a fallback for 'undefined'. Quite often they will be used in combination, but they are independent. > I always declare my variables. So, I don't want to use isset() as they > will be an incorrect test. I use is_null(). Specifically testing the > value. If I've made a mistake and NOT declared the variable (or made a > typo), I want to know. I don't want to hide it with isset()/empty(). That's exactly why I think the conditional should use a !==null test, not an isset() test. Ben.