Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59887 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17627 invoked from network); 13 Apr 2012 16:12:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 16:12:58 -0000 Authentication-Results: pb1.pair.com smtp.mail=ekneuss@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ekneuss@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.45 as permitted sender) X-PHP-List-Original-Sender: ekneuss@gmail.com X-Host-Fingerprint: 209.85.210.45 mail-pz0-f45.google.com Received: from [209.85.210.45] ([209.85.210.45:63509] helo=mail-pz0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5C/5F-35770-480588F4 for ; Fri, 13 Apr 2012 12:12:55 -0400 Received: by dacx6 with SMTP id x6so4072846dac.18 for ; Fri, 13 Apr 2012 09:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=eD4M13++yYkOjCLXZDPcRKdo5spwyVYmtXLh+kl6Xj0=; b=vARnmJHTJyBdaP60bq0aTfDImRftR8DPQKIt5jc8PsoQDoc8iXLJ9gv8QcWBqYPs1P xruyHyY++ATHHobfZsONC8zv145yeHPeFjrrKYRbetdUIFnM0DO7HKPLISQ3O6kTghUz EKhW4X+t9+mLT+SdB8hkyC16Rqhx4pby2WwxyBQb3+igJJWAhQub5HvvUOPWYZi3irb1 /YUJEyN/NJgcf0A6NGGgCt8sava0kceVYVSymcMMFvde/kftacxdbwwsJRA95PB57Ze1 jQmbLA9Zv7LCo5xno+LH/2PcfExmI0s39fkiq7ZGBsGCfp9wLYdmLyabgsuM8MN9n5iu ucXQ== MIME-Version: 1.0 Received: by 10.68.221.35 with SMTP id qb3mr5649457pbc.136.1334333568995; Fri, 13 Apr 2012 09:12:48 -0700 (PDT) Sender: ekneuss@gmail.com Received: by 10.68.72.6 with HTTP; Fri, 13 Apr 2012 09:12:48 -0700 (PDT) In-Reply-To: References: <1334271932.4609.161.camel@guybrush> Date: Fri, 13 Apr 2012 18:12:48 +0200 X-Google-Sender-Auth: gmWs6rDC1tV2XNE5C7WVEThmTQw Message-ID: To: Pal Konyves Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Allow non-variable arguments to empty() From: colder@php.net (Etienne Kneuss) On Fri, Apr 13, 2012 at 16:22, Pal Konyves wrote: > Well, if you want to use empty in the above mentioned situations, you mig= ht > need the change. I personally don't like using empty(). I use it only on > arrays, that's because semantically fits: array is empty. > > In other situations I prefer comparing against the according return type > because it improves code readability. Dynamic typing in PHP one of the > reasons of messy code anyways. > > I'm gonna try to give you alternatives of design for your examples below: > >> Browsing around Google it seems that one of the most common source of >> the function call in write context error is people trying to do >> if(empty(trim($xyz)). You don't necessarily need the result of >> trim($xyz), so it's reasonable not to save it into a temporary >> variable. > > if( trim($xyz) =3D=3D=3D ""); // as I talked about semantics and types ab= ove > >> >> Another example for example would be a function like this: >> >> public function isValid() { >> =C2=A0 =C2=A0if (...) { $this->addError('xyz'); } >> =C2=A0 =C2=A0if (...) { $this->addError('abc'); } >> =C2=A0 =C2=A0if (...) { $this->addError('foo'); } >> =C2=A0 =C2=A0if (...) { $this->addError('bar'); } >> >> =C2=A0 =C2=A0return empty($this->getErrors()); > > return $this->getErrorsCount(); > >> } >> >> Furthermore you don't necessarily have to throw the return value away. >> For example I commonly write code like this: >> >> if (null =3D=3D=3D $result =3D $this->foo()) { >> =C2=A0 =C2=A0throw new Exception(...); >> } >> $result->doSomething(); > > I used to use this kind of formula, but I realised It neither improves > performance nor readability, especially if you want to use $result after > the if statement. > >> >> You can do something similar with empty(): >> >> if (empty($values =3D $this->getValues()) { >> =C2=A0 =C2=A0return; >> } >> $this->doSomethingWith($values); > > I haven't try this but I assume this code is valid by precedence order. I > might be wrong, but still my above argument exist. > >> >> I know, not everyone likes that kind of coding style, but I think it >> has it's uses. > > > Etienne Kneuss: > " People typically assume empty checks for the empty > string here, and it doesn't." -- why not? What I meant is that when people do: $str =3D trim($foo); if (empty($str)) { } they typically want to check if the string is empty, i.e. contains 0 characters. They usually forget that empty() will also return true on the string "0". > > > Pal --=20 Etienne Kneuss http://www.colder.ch