Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51894 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33569 invoked from network); 14 Apr 2011 12:41:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2011 12:41:39 -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.91.228 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.91.228 nm25-vm0.bullet.mail.sp2.yahoo.com Received: from [98.139.91.228] ([98.139.91.228:26448] helo=nm25-vm0.bullet.mail.sp2.yahoo.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F8/01-25347-28BE6AD4 for ; Thu, 14 Apr 2011 08:41:39 -0400 Received: from [98.139.91.64] by nm25.bullet.mail.sp2.yahoo.com with NNFMP; 14 Apr 2011 12:41:35 -0000 Received: from [98.139.91.4] by tm4.bullet.mail.sp2.yahoo.com with NNFMP; 14 Apr 2011 12:41:35 -0000 Received: from [127.0.0.1] by omp1004.mail.sp2.yahoo.com with NNFMP; 14 Apr 2011 12:41:35 -0000 X-Yahoo-Newman-Id: 591068.47528.bm@omp1004.mail.sp2.yahoo.com Received: (qmail 31189 invoked from network); 14 Apr 2011 12:41:35 -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=BBvri5Ca5s2EnFOpD9X6+4vBBKVEtP/jWZGhIIlPWTjUYygkHs70/0nQLF80U7fU5F0RsIyKcVZ1Y5op5YR7wMgvacUIcvnhqV8v7Rik1rPxeFXm1NXfh2yE6LWa6Vz9IUcWIr2YuRRy/1prN7cMdnto8HYEBJNVhMiChAVsbHo= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com.au; s=s1024; t=1302784895; bh=ubaY4gGNwC8uG4OXIpLVnE+W7KuzGzkwyNtk4iZLhOg=; 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=BiioMyEYvs3x96lqSVbxnuHd1PB0GKHd0RDLeukZwN/lPT8A5wrLOl7vozLbrqpG3QZ2zfNB9S8a8yiejGIFCiWZjZiEYupChqhP/bpw8peZB3okrNROIwr2mDvVEQrL1uzs8fsId1fomRX/xl7sneic511yWqFA8N4QQLKHX40= Received: from thought.local (mail_ben_schmidt@124.168.69.175 with plain) by smtp132.mail.mud.yahoo.com with SMTP; 14 Apr 2011 05:41:34 -0700 PDT X-Yahoo-SMTP: enFMnPSswBAexaHyzgobwuUTrYOhZdJ0KRA2SjA- X-YMail-OSG: oeTdOSEVM1kRjCOriz1KT6Tvie3eQ1ZmpnOsifF3Hvosksw 5SzxlTBBlNIEIId1vrFMM.tSDLg7P6gCWirq1YD07WlazwEYg3lsyvWntoFK ngHWIoP4gaD6WFUCtDSbzGEn11nshNqgbzQJ.bjH6Flfgy_4QvG3GKkHUOjs Z4kK1pWAqV_vgJa1zjkGN98atLei4BiPpHjnQ.dFshB.AWXRA_zamxxv5xO1 tAw6Sq8DQIyUJ.1i7u6TpMiMa8vTbKHvX2mBgdVi3DiYwYfKkauMLhR7aShG .MBddAWTljUpApxd2bvjQ0cKYGdGf2xOMsPFKW4gRScI5HAuSwwACq7nzwCs ePwxOlajHAZoMc9kRNoe.yCkOJS0- X-Yahoo-Newman-Property: ymail-3 Message-ID: <4DA6EB84.8000205@yahoo.com.au> Date: Thu, 14 Apr 2011 22:41:40 +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: Jordi Boggiano CC: 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> <4DA6E151.4030707@yahoo.com.au> <4DA6E3CC.2090100@seld.be> In-Reply-To: <4DA6E3CC.2090100@seld.be> 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) On 14/04/11 10:08 PM, Jordi Boggiano wrote: > On 14.04.2011 13:58, Ben Schmidt wrote: >> I have many, many uses for this. E.g. patterns like this: >> >> class Foo { >> private $defaultBar; >> public function foobar(Bar $bar=null) { >> $bar = isset($bar) ? $bar : $this->defaultBar; >> $bar->doSomething(); >> } >> } > > I'm sorry but this is not a valid use case, isset() is totally > unnecessary here. You can use > > $bar = $bar ?: $this->defaultBar; > > Of course it still repeats $bar, but it's not that verbose, and will > work exactly the same as what you do. I'd argue it's even better than > what you do because, if it weren't for that Bar type hint, if someone > passes false or "blah" to your function, isset() will return true, and > you'll try to call doSomething on false/"blah", which is a cute fatal error. Actually, you'll get a cute fatal error when the function is called because of the type hint in that case. Anyway, yeah, sure, perhaps a bad example in its detail. I didn't spend hours composing it. Just use a little imagination, though, and imagine that it wasn't a type hinted object coming in, but some other value like an integer. Then a boolean test would not work (would be false for zero and erroneously use the default value). You then need the isset test. I personally prefer to always use an isset/key_exists/is_null test when I'm testing for something's 'existence' and not its 'truth'. I think that makes the code more readable. Ben.