Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59883 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94134 invoked from network); 13 Apr 2012 14:22:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 14:22:38 -0000 Authentication-Results: pb1.pair.com smtp.mail=paul.konyves@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=paul.konyves@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.170 as permitted sender) X-PHP-List-Original-Sender: paul.konyves@gmail.com X-Host-Fingerprint: 209.85.213.170 mail-yx0-f170.google.com Received: from [209.85.213.170] ([209.85.213.170:48692] helo=mail-yx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 63/0B-35770-DA6388F4 for ; Fri, 13 Apr 2012 10:22:37 -0400 Received: by yenl5 with SMTP id l5so1857402yen.29 for ; Fri, 13 Apr 2012 07:22:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=TjIIBLE+sMVJtThirptAX5XKEm2iMdfBms7j2AQnktI=; b=ZjCl4AnYVyELn4xueyAgO923T9m0We27qy/E7NrW2ajO/wnONlYxF3WbWk83TLrqHK Y4bYqa22A+mtFNsX///kg6ACNn8xOxhItIhMeJVfrr8Kcra0uJ3uYl84YZROB/ueGc5T aJjxDv1N/juZ8ohdsO3HtreLoJrQUJ+hwUyA83AzGeUoW69nKvkugJWRUUmIUsyz1vEp 8/6Z45skoNgk49PAGyQOorzQN4lA7PHLGPKtxzMI/hnCVo0FMXE4yFHgH0nDlKlx3sMd txlAMRQQ/dxXB9pehzmaqDO2J1fDf4ZUpiyEDrgPD5ywYdj6IInCIGDCqOft+7Ixfnv3 3upQ== Received: by 10.236.193.36 with SMTP id j24mr1735203yhn.34.1334326955050; Fri, 13 Apr 2012 07:22:35 -0700 (PDT) MIME-Version: 1.0 Received: by 10.147.182.20 with HTTP; Fri, 13 Apr 2012 07:22:14 -0700 (PDT) In-Reply-To: References: <1334271932.4609.161.camel@guybrush> Date: Fri, 13 Apr 2012 16:22:14 +0200 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf305b0c0e2b84dd04bd9034d4 Subject: Re: [PHP-DEV] Allow non-variable arguments to empty() From: paul.konyves@gmail.com (Pal Konyves) --20cf305b0c0e2b84dd04bd9034d4 Content-Type: text/plain; charset=UTF-8 Well, if you want to use empty in the above mentioned situations, you might 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) === ""); // as I talked about semantics and types above > > Another example for example would be a function like this: > > public function isValid() { > if (...) { $this->addError('xyz'); } > if (...) { $this->addError('abc'); } > if (...) { $this->addError('foo'); } > if (...) { $this->addError('bar'); } > > return 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 === $result = $this->foo()) { > throw 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 = $this->getValues()) { > return; > } > $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? Pal --20cf305b0c0e2b84dd04bd9034d4--