[php-src] Issue #7845: Stop to automatically cast numeric-string to int when using them as array-key

From: Date: Tue, 28 Dec 2021 19:19:39 +0000
Subject: [php-src] Issue #7845: Stop to automatically cast numeric-string to int when using them as array-key
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238615@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7845 Comment Author: VincentLanglet > Basically, this behavior is there to make [0 => > 'foo']/['0' => > 'foo'] and > $array[0]/$array['0'] > equivalent. I wasn't sure if it was intentional or if it was some weird things like 0 == "foo" prior to php8. But I don't see useful case of $array[0]/$array['0'] equivalence. > Even if it's possible to persist the key types while keeping this behavior the same, any > change in arrays will likely lead to countless subtle bugs in countless PHP code bases. I would say that on the opposite that this behavior may currently be an easy way to introduce a bug in code base with array_flip changing the type or array_search not returning the expected type. I don't know how but this could be indeed interesting to see how big would be the impact of this change. But even if it has some impacts, it should still be considered (a huge impact didn't prevent https://wiki.php.net/rfc/deprecate_dynamic_properties to be accepted). To me, it's a follow-up of https://wiki.php.net/rfc/string_to_number_comparison and https://wiki.php.net/rfc/saner-numeric-strings. > There's also no good migration path to make users aware of this chance. Given ``` $foo = ['0' => 1]; $foo['0']; $foo[0]; $bar = [0 => 1]; $bar['0']; $bar[0]; $foo == $bar; // true ``` What if $foo[0] trigger a deprecation: "The key 0 was defined as a string ; please cast the key to string before accessing to the value or update the array key" And $bar['0'] trigger a deprecation: "The key '0' was defined as an int ; please cast the key to int before accessing to the value or update the array key" This won't change anything but force people to update the code to ``` $foo = ['0' => 1]; $foo['0']; $foo[(string) 0]; $bar = [0 => 1]; $bar[(int) '0']; $bar[0]; ``` which would work before and after the BC-break.

« previous php.bugs (#238615) next »