Re: [RFC] Allow non-scalar keys in foreach
| From: | Etienne Kneuss | Date: | Tue, 19 Feb 2013 14:12:16 +0000 |
| Subject: | Re: [RFC] Allow non-scalar keys in foreach | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-65953@lists.php.net to get a copy of this message | ||
On Tue, Feb 19, 2013 at 2:55 PM, Derick Rethans <derick@php.net> wrote:
> Gah, no top posting!
>
> On Tue, 19 Feb 2013, Etienne Kneuss wrote:
>
> > On Tue, Feb 19, 2013 at 2:25 PM, Derick Rethans <derick@php.net> wrote:
> >
> > > On Tue, 19 Feb 2013, Nikita Popov wrote:
> > >
> > > > This RFC proposes to remove the type-restrictions on Iterator keys
> > > > used in foreach:
> > > >
> > > > https://wiki.php.net/rfc/foreach-non-scalar-keys
> > > >
> > > > I took over Levi's RFC and added a patch for it.
> > >
> > > Under "Open Questions" you write:
> > >
> > > > What should be done with the keys that are valid in the iterator,
> > > > but not in the array? I think the best approach would be to just
> > > > set the array keys with the exact same semantics as PHP would do
> > > > (i.e. with all casts and warnings).
> > >
> > > Would __toString be called in case the key was an object?
>
> > 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.