Req #72207 [Opn->Csd]: Don't allow numeric object keys

From: Date: Thu, 18 May 2017 08:11:21 +0000
Subject: Req #72207 [Opn->Csd]: Don't allow numeric object keys
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209176@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72207&edit=1 ID: 72207 Updated by: nikic@php.net Reported by: remyfox at hotmail dot com Summary: Don't allow numeric object keys -Status: Open +Status: Closed Type: Feature/Change Request Package: Class/Object related Operating System: Windows PHP Version: 7.0.6 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: This will be fixed by https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts in PHP 7.2. Previous Comments: ------------------------------------------------------------------------ [2016-05-17 12:08:29] cmb@php.net > I know that the manual states that numeric object keys are > inaccessible, but this *feature* seems a little clumsy. Anyhow, it is not a bug, so I'm changing this ticket to a feature request. Note that changing the current behavior would constitute a BC break, so an RFC would be necessary, see <https://wiki.php.net/rfc/howto>. ------------------------------------------------------------------------ [2016-05-12 09:31:13] remyfox at hotmail dot com Description: ------------ Objects don't seem to allow numeric keys. However, when arrays are converted into objects, then numeric, inaccessible object keys appear - which is inconsistent. By the way I know of no way to create float, boolean or null keys. I know that the manual states that numeric object keys are inaccessible, but this *feature* seems a little clumsy. http://php.net/manual/en/language.types.object.php#language.types.object.casting The PHP team should make a decision here: 1. either don't allow numerical keys in objects (when converting from arrays) or 2. stop converting all scalars into strings (for object keys) and stop converting all floats, numeric strings and booleans into integers (for array keys). Array and object getter and setter comparisons would become strict. Another current issue is the fact that it is possible to use null as array key (null is converted into ""), but it remains impossible to use null as object key (despite conversions that do happen on floats, booleans and integers). If you decide to convert all types into string object keys, then why not include null as well? Although the first option (all array keys are of the numeric=integer or string type and all object keys are of the string type) is closer to the status quo, in the long term the second option seems to be the cleanest and clearest. Test script: --------------- // Example 1 $object = (object) [123 => "value1"]; $object->{"123"} = "value2"; // Prints object(stdClass)#1 (2) { [123]=> string(6) "value1" ["123"]=> string(6) "value2" } var_dump($object); // Prints string(6) "value2" var_dump($object->{123}); // Prints string(6) "value2" var_dump($object->{"123"}); // Example 2 $object1 = (object) [123 => "value"]; $object2 = new StdClass(); $object2->{123} = "value"; // Prints object(stdClass)#2 (1) { [123]=> string(5) "value" } var_dump($object1); // Prints object(stdClass)#3 (1) { ["123"]=> string(5) "value" } var_dump($object2); // Prints bool(false) var_dump($object1 == $object2); // Error var_dump($object1->{123}); // Prints string(5) "value" var_dump($object2->{123}); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72207&edit=1

« previous php.bugs (#209176) next »