Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59880 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85850 invoked from network); 13 Apr 2012 13:37:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 13:37:43 -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:40196] helo=mail-pz0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0A/79-35770-62C288F4 for ; Fri, 13 Apr 2012 09:37:42 -0400 Received: by dacx6 with SMTP id x6so3889925dac.18 for ; Fri, 13 Apr 2012 06:37:39 -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=POklIry17BVlDuyjbuZS9d4G2L/ywm1f7d0udhhImCg=; b=FSvKIRayvosgpvqxWE7magnZSo6n3PSIb168uGJ9MMVDrr935z4RgXnok5NUQBafwL JlGLVH/YCzrDRk+j3ifJYBq8DTQgGd4xIgmrqp7aZPhn9PIYSWhar5SsxvPj44s5B0bI F0Wd1aO5XEPQkI2xteNa4MP81i/2ASjF2zzv3WEnD9ylrPXAgJE7uwN1nwijo5EHG3gu XVYFMppNoyVgEpOu5EwtEqMp9dCSTiQmsiLpAwJBsliguyiyHVEV3kxgQDjB/xrpFXjq c7J+Fk6k02J1Ew1z0nKfM+i9TdlP/SGyNyP1NrNumwcPtEaCIy1hRLC9607hW/JGUtud gMkQ== MIME-Version: 1.0 Received: by 10.68.203.9 with SMTP id km9mr2253102pbc.94.1334324258814; Fri, 13 Apr 2012 06:37:38 -0700 (PDT) Sender: ekneuss@gmail.com Received: by 10.68.72.6 with HTTP; Fri, 13 Apr 2012 06:37:38 -0700 (PDT) In-Reply-To: References: <1334271932.4609.161.camel@guybrush> Date: Fri, 13 Apr 2012 15:37:38 +0200 X-Google-Sender-Auth: 3k4NjxB3behbCGjBSef79ytQdXI Message-ID: To: Nikita Popov Cc: Pal Konyves , 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) Hi, On Fri, Apr 13, 2012 at 15:00, Nikita Popov wro= te: > On Fri, Apr 13, 2012 at 11:37 AM, Pal Konyves wr= ote: >> I don't see the point of empty( function() ). >> >> You tipically use empty on values that holds information you want to use >> later in the program flow (a string, an integer). That means you'd bette= r >> extract it to a variable because you want to avoid calling twice a funct= ion >> that provides this value. > You don't always need the value. > > 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. well, empty(trim()) actually is a good example for when you don't want to use empty(). People typically assume empty checks for the empty string here, and it doesn't. > > 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()); > } > > 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(); > > You can do something similar with empty(): > > if (empty($values =3D $this->getValues()) { > =C2=A0 =C2=A0return; > } > $this->doSomethingWith($values); > > I know, not everyone likes that kind of coding style, but I think it > has it's uses. I believe the change is good, but only because it removes an annoying error condition that is hard to figure out for beginners. Other than that I don't believe it is useful though: people that know what empty() really does can use "!expr" when expr is not a variable, which is less confusing, especially for strings. Best, > > Nikita > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > --=20 Etienne Kneuss http://www.colder.ch