Bug #72207 [NEW]: Don't allow numeric object keys

From: Date: Thu, 12 May 2016 09:31:16 +0000
Subject: Bug #72207 [NEW]: Don't allow numeric object keys
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201029@lists.php.net to get a copy of this message
From: remyfox at hotmail dot com Operating system: Windows PHP version: 7.0.6 Package: Class/Object related Bug Type: Bug Bug description:Don't allow numeric object keys 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 bug report at https://bugs.php.net/bug.php?id=72207&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72207&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72207&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72207&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=72207&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=72207&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=72207&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=72207&r=needscript Try newer version: https://bugs.php.net/fix.php?id=72207&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=72207&r=support Expected behavior: https://bugs.php.net/fix.php?id=72207&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=72207&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=72207&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=72207&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72207&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=72207&r=dst IIS Stability: https://bugs.php.net/fix.php?id=72207&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=72207&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=72207&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=72207&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=72207&r=mysqlcfg

« previous php.bugs (#201029) next »