php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72207 Don't allow numeric object keys
Submitted: 2016-05-12 09:31 UTC Modified: 2017-05-18 08:11 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: remyfox at hotmail dot com Assigned: nikic (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.0.6 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: remyfox at hotmail dot com
New email:
PHP Version: OS:

 

 [2016-05-12 09:31 UTC] 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});


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-05-17 12:08 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request
 [2016-05-17 12:08 UTC] 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>.
 [2017-05-18 08:11 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-05-18 08:11 UTC] nikic@php.net
This will be fixed by https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts in PHP 7.2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 00:01:32 2024 UTC