Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96738 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12259 invoked from network); 5 Nov 2016 20:56:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Nov 2016 20:56:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@ohgaki.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@ohgaki.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ohgaki.net designates 180.42.98.130 as permitted sender) X-PHP-List-Original-Sender: yohgaki@ohgaki.net X-Host-Fingerprint: 180.42.98.130 ns1.es-i.jp Received: from [180.42.98.130] ([180.42.98.130:60588] helo=es-i.jp) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 68/DC-07826-6774E185 for ; Sat, 05 Nov 2016 15:56:24 -0500 Received: (qmail 125541 invoked by uid 89); 5 Nov 2016 20:56:18 -0000 Received: from unknown (HELO mail-wm0-f46.google.com) (yohgaki@ohgaki.net@74.125.82.46) by 0 with ESMTPA; 5 Nov 2016 20:56:18 -0000 Received: by mail-wm0-f46.google.com with SMTP id f82so51196826wmf.1 for ; Sat, 05 Nov 2016 13:56:18 -0700 (PDT) X-Gm-Message-State: ABUngvc3BaI88fyL6QDkRO9BrDbLRo6eXojBAk4he6UJQUL2PJ06XF6gC7cXr2/9rKj1gsPwJbqMMmKESto2rQ== X-Received: by 10.194.24.34 with SMTP id r2mr16209481wjf.111.1478379371469; Sat, 05 Nov 2016 13:56:11 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.205.42 with HTTP; Sat, 5 Nov 2016 13:55:30 -0700 (PDT) In-Reply-To: References: Date: Sun, 6 Nov 2016 05:55:30 +0900 X-Gmail-Original-Message-ID: Message-ID: To: Andrea Faulds Cc: "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [VOTE] Convert numeric keys in object/array casts From: yohgaki@ohgaki.net (Yasuo Ohgaki) Hi Andrea, On Sun, Nov 6, 2016 at 1:30 AM, Andrea Faulds wrote: > Two weeks have passed since this RFC was put to discussion here, and no > significant issues have cropped up. Therefore, I'm going to put it to a vote > for inclusion in PHP 7.2. > > Voting starts today, 2016-11-05, and ends the Monday after next, 2016-11-14. > > The RFC and voting widget can be found here: > https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts > > It's a normal 2/3 majority required vote. In short, array int index is converted to string numeric name property, vice versa. Correct? At first, I thought this is good idea, but it seems we are better to allow "string integer" array key access (array_get/set_var(), perhaps) and change other related features accordingly. Currently, inaccessible value could happen in array due to "int like string conversion to int" also. https://3v4l.org/EpDuo Line 9: var_dump($tmp, $tmp[0], $tmp['0']); outputs array(4) { [0]=> int(5) [1]=> int(6) [2]=> int(7) ["0"]=> string(3) "zzz" // <== String '0' indexed element is Inaccessible } int(5) int(5) // <== Only long index 0 can be accessed Either before or after RFC has pros and cons. For instance, proposed change will require string casts for numeric property iteration, correct? for($i=0; $i < 100; $i++) { $str_i = (string)$i; // <== "int" key to "string" key conversion // requires cast. echo $obj->{$str_i}; // This kind of expression with int/int like string // index is not allowed now, but this // could be valid. // https://3v4l.org/e5L1T } Another cons after RFC is BC that $arr[0] = 123; $obj=(object)$arr; $obj->{0} became $obj->{'0'} Simply allowing access to "numeric string index and long index" for _both_ array and object seems cleaner resolution for problems we have. (Object allows distinct access to 0 and '0' indexes already, so make array allow to access 0 and '0' indexed element.) e.g // There is no way to get string '0' index element now, so add // array_get_var(); $obj->{0} === $arr[0]; $obj->{'0'} === array_get_var($arr, '0'); // Get string '0' indexed value // Int and string index can have distinct value $arr[0] !== $arr['0']; $obj->{'0'} !== $arr[0]; $obj->{0} !== array_get_var($arr, '0'); $obj->{0} !== $obj->{'0'}; // Currently, we don't have way to set string '0' indexed element except // converting object with string '0' index (e.g. $obj->{'0'} = 123;) // to array. So implement array_set_var() to allow array to have // string '0' indexed element. array_set_var($arr, '0', 123); // Set string '0' indexed value 123 $obj = (object)$arr; $ojb->{'0'} === array_get_var($arr, '0'); Making array accessible to "string int index" and reorganize other features accordingly seems result in more consistent spec. What do you think? I found interesting behaviors while testing. I don't think I have good understanding of this issue yet, please point out if I miss/misunderstand something. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net