Re: [RFC] Allow non-scalar keys in foreach
| From: | Levi Morrison | Date: | Tue, 19 Feb 2013 17:36:43 +0000 |
| Subject: | Re: [RFC] Allow non-scalar keys in foreach | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-65973@lists.php.net to get a copy of this message | ||
>> > I think the warning can stay as-is, __toString is not necessarily
>> > available so casting to string in all occasions is probably not what
>> > we want.
>>
>> I think it should cast to a string if possible. You are now making
>> iterator_to_array not work with the new feature, and I find that a bit
>> silly. Whether it should be __toString (or __toKey) I don't really care,
>> but this new support for objects should be supported everywhere (and
>> that includes iterator_to_array).
>>
>
> I think this RFC is orthogonal to iterator_to_array supporting conversions
> from objects to keys.
>
> $array[$obj] = 42; does not call __toString (or __toKey), it throws a
> catchable fatal. IMO it would be inconsistent if iterator_to_array
> gracefully accepted objects as keys.
Personally I think the real problem here is that
iterator_to_array
should *not* copy keys by default. However, what's done is done and we
need to thoughtfully consider this situation.
You already CAN return objects from an iterator; you just can't use it
in a foreach. If you try to use that iterator in iteator_to_array
you get a lovely warning:
Warning: Illegal type returned from %s::key() in %s on line %d
I'm all for *not* changing that behavior; if they want it ignore keys
then they can pass false as a second parameter to
iterator_to_array.