Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81000 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78667 invoked from network); 22 Jan 2015 22:58:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Jan 2015 22:58:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.27 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.27 out3-smtp.messagingengine.com Received: from [66.111.4.27] ([66.111.4.27:45819] helo=out3-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 56/83-61273-18081C45 for ; Thu, 22 Jan 2015 17:58:10 -0500 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 2B437212D3 for ; Thu, 22 Jan 2015 17:58:07 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute5.internal (MEProxy); Thu, 22 Jan 2015 17:58:07 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=x-sasl-enc:message-id:date:from :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=awccoiXkXOrm9SLfJ42Uvf e9L/k=; b=NYuE/nmHJXD1TubMX/oGPz9dVI7uO4FD264Ra0CS4RXz1NapUFHSqb x3NCFqPTNt587a/3wlhsIW4U1JFfk5srFqzbLgy87uYJNy0Mp/80Y7ATqmz1CRo4 ryw9anZDYCFvyczz14If0NeYFRhlnqCEEGxVCJfFzSyyMhUrQW30A= X-Sasl-enc: enpWhSdjiOSMpDBz24o5WKscKXhyxisw6y95wc7x0Bzj 1421967486 Received: from [192.168.42.117] (unknown [98.226.241.18]) by mail.messagingengine.com (Postfix) with ESMTPA id E8704680117 for ; Thu, 22 Jan 2015 17:58:06 -0500 (EST) Message-ID: <54C1807E.2050004@garfieldtech.com> Date: Thu, 22 Jan 2015 16:58:06 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net References: <54BE7749.2010603@gmail.com> In-Reply-To: <54BE7749.2010603@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] in Operator From: larry@garfieldtech.com (Larry Garfield) On 01/20/2015 09:42 AM, Rowan Collins wrote: > Mike Willbanks wrote on 20/01/2015 03:30: >> I am very familiar with the in operator. However, the implementation >> would be incomplete without handling loops via the in operator. Many >> people when seeing an in operator also think of JavaScript. In that >> case >> the in operator iterates over properties. As such in PHP we should >> be able >> to iterate over associative arrays should the syntax be added. >> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in >> > > I don't really see how this is related to the in operator, other than > that some languages reuse the same keyword for both purposes. > > My immediate association when I saw this proposal was not "ah, like > the loop syntax in JavaScript", but "ah, like the IN() operator in SQL". > > Given the ability to write foreach ( $foo as $key => $value ), I'm not > sure adding a variant syntax of for ( $key in $foo ) has that much > value for PHP. > > Regards, I like the idea of a boolean IN. If nothing else it would simplify the common cases for strpos and in_array() and friends. I agree that leaving out for/foreach is a good idea as that's a different thing that would just so happen to use the same new reserved word. That's fine; "use" became reserved for namespaces and then was reused for traits without incident. However, we should also ask what happens with objects. I've seen a lot of people trying to push toward collection objects rather than plain arrays for a variety of reasons, and of course we run smack into the "PHP has all of these cool array-manipulation functions that are almost great for functional programming but don't work on iterators, ah crap" problem. So what would the following do: $o = new stdclass; $o->a = 'b'; $o->c = 'd'; print 'a' in $o; print 'b' in $o; Or more to the point: $a = new ArrayObject(['a', 'b', 'c']); print 'c' in $a; There are likely lots of implementation details around making it work, but it would be lovely if at least some collection objects could support IN. Perhaps an Innable interface? :-) (Which then an iterator could also implement if it made logical sense for it to do so, which is not always the case.) --Larry Garfield