# [RFC] Convert numeric keys in object/array casts

- Thread: https://externals.io/message/96546
- Original archive: https://news-web.php.net/php.internals/96546
- Started by Andrea Faulds on 2016-10-21T23:11:41+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #96546 — Andrea Faulds — 2016-10-21T23:11:41+00:00

URL: https://externals.io/message/96546#96546

Hi everyone,

To try and fix a longstanding issue, and to actually bring some 
attention to the fix, I've created a new RFC, which can be found here:

https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts

It targets PHP 7.2, which is maybe a little conservative. It could 
target PHP 7.1 theoretically, but it's probably a bit late for that.

I'm also not sure if this really ought to be an RFC, but I've received 
little feedback so far, so it can't hurt.

Anyway, please tell me your thoughts!
-- 
Andrea Faulds
https://ajf.me/

---

## #96548 — Yasuo Ohgaki — 2016-10-22T19:59:38+00:00

URL: https://externals.io/message/96546#96548 · In reply to: #96546

Hi Andrea,


> To try and fix a longstanding issue, and to actually bring some attention to
> the fix, I've created a new RFC, which can be found here:
>
> https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
>
> It targets PHP 7.2, which is maybe a little conservative. It could target
> PHP 7.1 theoretically, but it's probably a bit late for that.
>
> I'm also not sure if this really ought to be an RFC, but I've received
> little feedback so far, so it can't hurt.

Nice RFC!

It seems patch is made to convert int index to string index to allow
numeric key, is it? I guess it is the limitation and the reason why
it's inaccessible.

Numeric key name must be string?

$obj->{'0'} = 1;

or could be like (Without quotes)

$obj->{0} = 1;


Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #96549 — Yasuo Ohgaki — 2016-10-22T20:46:55+00:00

URL: https://externals.io/message/96546#96549 · In reply to: #96548

> Numeric key name must be string?
>
> $obj->{'0'} = 1;
>
> or could be like (Without quotes)
>
> $obj->{0} = 1;

It seems variables can be numeric now.

https://3v4l.org/bjZ4d

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #96555 — Davey Shafik — 2016-10-23T10:54:10+00:00

URL: https://externals.io/message/96546#96555 · In reply to: #96549

This has been the case since 4.3 at the least: https://3v4l.org/bjZ4d


> 
> > […]
>
> It seems variables can be numeric now.
>
> https://3v4l.org/bjZ4d
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #96560 — Andrea Faulds — 2016-10-23T15:23:17+00:00

URL: https://externals.io/message/96546#96560 · In reply to: #96548

Hi Yasuo,

Yasuo Ohgaki wrote:
> Nice RFC!
>
> It seems patch is made to convert int index to string index to allow
> numeric key, is it?

Or vice-versa, yes.

> I guess it is the limitation and the reason why
> it's inaccessible.

Yes. If you have an object whose HashTable has an integer key, say 123, 
and you try to look up the object property 123, it will look for the 
string key "123", and will not find it.

> Numeric key name must be string?
>
> $obj->{'0'} = 1;
>
> or could be like (Without quotes)
>
> $obj->{0} = 1;

Both are supported, either way it converts to a string key.

-- 
Andrea Faulds
https://ajf.me/

---

## #96567 — Stanislav Malyshev — 2016-10-23T23:47:22+00:00

URL: https://externals.io/message/96546#96567 · In reply to: #96546

Hi!

> To try and fix a longstanding issue, and to actually bring some
> attention to the fix, I've created a new RFC, which can be found here:
> 
> https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts

How common is this problem? It looks like an edge case of an edge case
(converting objects to arrays as such is not a very frequent operation,
and the reverse is even less frequent) but adds performance hit on the
common case.

> I'm also not sure if this really ought to be an RFC, but I've received
> little feedback so far, so it can't hurt.

This is a behavior change and a performance hit, so definitely yes, it
should be RFC.
-- 
Stas Malyshev
smalyshev@gmail.com
