Bug #67640 [Com]: Object to array cast results in duck typing
| From: | mightyuhu@php.net | Date: | Fri, 18 Jul 2014 11:04:21 +0000 |
| Subject: | Bug #67640 [Com]: Object to array cast results in duck typing | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186724@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67640&edit=1
ID: 67640
Comment by: mightyuhu@php.net
Reported by: php at kingsquare dot nl
Summary: Object to array cast results in duck typing
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: n/a
PHP Version: 5.5.14
Block user comment: N
Private report: N
New Comment:
In this case i'd expect $array to look like
array(1) { [0]=> string(5) "test1" }
instead of
array(1) { ["0"]=> string(5) "test1" }
test code:
<?php
$json = '{ "0" : "test1" }';
// just decode that into an object
$obj = json_decode($json);
// recast it into an array
$array = get_object_vars($obj);
// just dump to see the results
var_dump($obj,$array, $array[0], $array['0']);
//object(stdClass)#1 (1) { ["0"]=> string(5) "test1" } array(1) { [0]=>
string(5) "test1" } string(5) "test1" string(5) "test1"
$array2 = (array) $obj;
var_dump($obj,$array2, $array2[0], $array2['0']);
//E_NOTICE : type 8 -- Undefined offset: 0 -- at line 10
E_NOTICE : type 8 -- Undefined index: 0 -- at line 10
$array3 = json_decode($json,true);
var_dump($array3, $array3[0], $array3['0']);
//object(stdClass)#1 (1) { ["0"]=> string(5) "test1" } array(1) {
["0"]=> string(5) "test1" } NULL NULL array(1) { [0]=> string(5)
"test1" } string(5) "test1" string(5) "test1"
?>
Previous Comments:
------------------------------------------------------------------------
[2014-07-17 10:12:55] php at kingsquare dot nl
Description:
------------
Creating objects from (i.e.) a JSON string and casting that to an array results in something that
looks like an array, acts like an array, but in reality is still an object? (or atleast suffers from
not supporting numeric properties on objects)
I know objects can't have numeric properties but dumping the object _and_ array works as
intended... then accessing the array by index 0 it fails? (but accessing the object property like
$obj->{"0"} works, thus i'd expect $array[0] / $array['0'] to work as
well ?
(see reports: 45346 / 61655 perhaps?)
I know about the lookup / juggling mismatch (the index is actually a string "0" but access
via index is juggled into an int and thus cant be found...) Although i understand the problem it has
some issues:
first and foremost: i expected it to work ;) (since creating an $foo =
array("1"=>"test") will work with $foo[1], so from a user perspective there
should be no difference
- this specific use case seems undocumented ?
- If var_dump-ing the resulting array i should be able to either use array_key_exists or by
derefencing to access the key ... If not: var_dump should not show it / mention it.. ;)
Test script:
---------------
$json = '{ "0" : "test1" }';
// just decode that into an object
$obj = json_decode($json);
// recast it into an array
$array = (array) $obj;
// just dump to see the results
var_dump($obj, $array, $array['0'], $array[0]);
Actual result:
--------------
Notices on the undefined index: 0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67640&edit=1