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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to