Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80852 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 56217 invoked from network); 20 Jan 2015 03:30:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jan 2015 03:30:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=pencap@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=pencap@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.180 as permitted sender) X-PHP-List-Original-Sender: pencap@gmail.com X-Host-Fingerprint: 209.85.223.180 mail-ie0-f180.google.com Received: from [209.85.223.180] ([209.85.223.180:55993] helo=mail-ie0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9A/B1-41341-DBBCDB45 for ; Mon, 19 Jan 2015 22:30:07 -0500 Received: by mail-ie0-f180.google.com with SMTP id rl12so1037715iec.11 for ; Mon, 19 Jan 2015 19:30:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=jlzRES3D/llyqXqySY5UVad8peZTAPcOJg+E+waUZgo=; b=RqXoGd6kwlsknWKeFLixH1U+torAdKUiTUwc8jGPP3I0VfCEJH2bQ7wH7vHDsux8mE yXBdS04qkQDMp7YFF+ziaRXFHpSjFTaCwOn0oA+U2R74D/IOJZWLDcDF6BgPXWKhGOvs nilDuOE4Z+sRhOcjWcq8+DYXzyogSLIo2Z4ACDOz990zO71uEH8ktrpqznaWwk4lPjKG olXxPq8MCh16LbmAhgCatCcyB+D0wZuuC4b5XfZ0fMbmpoKxB3Aic6uIXiFdcVIrJOXa s+3RqSqEee0BsQkP/lh7KOUKk0tLXZkrp/ZGu7SlDBWa0Lu9QF4sJq0HKgj4vvQQHB7J 1Z+w== MIME-Version: 1.0 X-Received: by 10.50.67.18 with SMTP id j18mr23537851igt.26.1421724603320; Mon, 19 Jan 2015 19:30:03 -0800 (PST) Received: by 10.50.248.45 with HTTP; Mon, 19 Jan 2015 19:30:03 -0800 (PST) In-Reply-To: References: Date: Mon, 19 Jan 2015 21:30:03 -0600 Message-ID: To: Niklas Keller Cc: PHP Internals Content-Type: multipart/alternative; boundary=047d7bdc0532f337d1050d0d0d9e Subject: Re: [PHP-DEV] in Operator From: pencap@gmail.com (Mike Willbanks) --047d7bdc0532f337d1050d0d0d9e Content-Type: text/plain; charset=UTF-8 Hello Niklas, On Sun, Jan 18, 2015 at 6:42 PM, Niklas Keller wrote: > Hello, > > I'd like to propose a new feature to PHP: The in Operator > Bob mentioned a few weeks ago he wants such an operator in PHP and today I > stumbled over > > http://nikic.github.io/2012/07/27/How-to-add-new-syntactic-features-to-PHP.html > again, > which uses the in operator as a sample. > 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 > Use cases: > > $obj = new StdClass; > $obj->prop = "value"; > $languages = ["PHP", "C", "Java"]; > var_dump("PHP" in $languages); // true > var_dump("Python" in $languages); // false > var_dump(0 in $languages); // false > var_dump("0x0" in ["0e3"]); // false > var_dump("prop" in $obj); // true > var_dump("foo" in $obj); // false > var_dump("Hello" in "Hello World!"); // true > > For strings, it would replace `strpos("Hello World!", "Hello") !== false)`, > for arrays it would replace `in_array($needle, $haystack, true)` > and for objects it would be a replacement of property_exists($class, > $property)`. > > This change would mainly be syntax sugar, but I think it's worth because: > * `"0x0" in ["0e3"]` would return false while `in_array("0x0", ["0e3"])` > returns true, that's probably an edge case, but there may be a lot of > people that don't use the `$strict` parameter, see http://3v4l.org/0K7E5 > * It would solve the issue that it's hard to remember the order of the > arguments for in_array and strpos, just had to look it up again. > I do think that it would be nice for a few different reasons but the in operator as such can also cause a bit of confusion and misdirection depending on how it is implemented. In the use cases above you do mention some inconsistencies with PHP functions that already exist. Additionally you did mention objects, in what scope would it properly leverage? My assumption would be visible properties from the context which you are in. Regards, Mike --047d7bdc0532f337d1050d0d0d9e--