Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51900 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46634 invoked from network); 14 Apr 2011 13:47:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2011 13:47:47 -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.212.185 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.212.185 nm26.bullet.mail.bf1.yahoo.com Received: from [98.139.212.185] ([98.139.212.185:44565] helo=nm26.bullet.mail.bf1.yahoo.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8E/F0-40161-10BF6AD4 for ; Thu, 14 Apr 2011 09:47:46 -0400 Received: from [98.139.212.150] by nm26.bullet.mail.bf1.yahoo.com with NNFMP; 14 Apr 2011 13:47:43 -0000 Received: from [98.139.212.199] by tm7.bullet.mail.bf1.yahoo.com with NNFMP; 14 Apr 2011 13:47:43 -0000 Received: from [127.0.0.1] by omp1008.mail.bf1.yahoo.com with NNFMP; 14 Apr 2011 13:47:43 -0000 X-Yahoo-Newman-Id: 718216.6173.bm@omp1008.mail.bf1.yahoo.com Received: (qmail 69953 invoked from network); 14 Apr 2011 13:47:43 -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=0knrbbHVg8abrhK2W3U2WUxqNfERyUNa1mu0lnjlMsl05m8HjmUDQEexzLF802pwLDR6m+t8yhRmnSwTFozVC1RBjbWADvuxra+F2PuJnZTaNlewt1YUOEr4+qtghWRIlT08HsbrKYU9JuLP6txLvtdQ1Sh6w7vnG1KIl6m96UA= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.au; s=s1024; t=1302788863; bh=UqRPiVYk0piffX3P5zHxeczcOYN0jGHlvZeWBuo8D2U=; 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=auh4hxLUKlUGmN4BFZFmjRuDDy9ZZOY0+YDHkLzlxnlqcTsZEKmlQoqEXlwJ7SurMhC0ts0gUn6GGFqy0SGc2cStZzfuGW9M8ZBla1wsBt3PxK+y2yJAmU6pMDPVlDiwzUE64x1IpMzJGiRHp+vN1rvz0h60tgnxUcheum9b5ME= Received: from thought.local (mail_ben_schmidt@124.168.69.175 with plain) by smtp132.mail.mud.yahoo.com with SMTP; 14 Apr 2011 06:47:42 -0700 PDT X-Yahoo-SMTP: enFMnPSswBAexaHyzgobwuUTrYOhZdJ0KRA2SjA- X-YMail-OSG: Ydx4KMAVM1kmg0PT9nX2roB1Rp0JJzlTuZTiyY9imG9N6A_ tgq.ktRjKmthdBiNYpZGSWdS7762ynFq4V2p25ZtttClH6qhQbza9dXFNglI Kxs_oN1Pzgoen_HwEA1G4BG1oNdew6DJaWzfea6IB.vIdjJQdk.lnJwfqmKe Tn4f2E.8HWwgPthptbE76OClyK_4F8c0Yh9ecOgy_tey5cMC6YQl3oAS.MBz jKdMQQ9m.Ni_EtbJ_ySZmEYOTgU8_2EAB5XDqu0KxdZEhLY63YAQALR9qYfo UIkl6WP_ALRvlDXrWusDxNJBI_frG_kZ9Tx2l8CidIIIplZNBRsIz62f_rV8 1XdHjZKfkNOmZlfI3u1s- X-Yahoo-Newman-Property: ymail-3 Message-ID: <4DA6FB03.9040404@yahoo.com.au> Date: Thu, 14 Apr 2011 23:47:47 +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: Martin Scotta CC: RQuadling@googlemail.com, Richard Quadling , Eloy Bote Falcon , Ole Markus With , "internals@lists.php.net" References: <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> <4DA6F2BC.10706@yahoo.com.au> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; 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) Sometimes this is true, but sometimes not. For example, HTTP GET/POST interfaces often have optional parameters. These need to be tested for, and often defaults provided, to write solid code. Saying you can always wrap it is like saying you can write your programs for a Turing machine. Also, not everyone is writing highly abstracted object-oriented code in PHP. Ben. On 14/04/11 11:25 PM, Martin Scotta wrote: > arrays are intent for holding values, not for represent things so use objects for > that. > the need for array_key_exists/isset to check for the presence of an index is a > smell that you need to refactor your code for a different datatype. > > If you cannot change the array, you always can wrap it. > > With good data abstractions the usage of array_key_exists/isset/empty is barely > reduced to the minimum. > > Martin Scotta > > > On Thu, Apr 14, 2011 at 10:12 AM, Ben Schmidt > wrote: > > 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. > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >