Bug #67640 [NEW]: Object to array cast results in duck typing
| From: | php at kingsquare dot nl | Date: | Thu, 17 Jul 2014 10:12:56 +0000 |
| Subject: | Bug #67640 [NEW]: Object to array cast results in duck typing | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-186686@lists.php.net to get a copy of this message | ||
From: php at kingsquare dot nl
Operating system: n/a
PHP version: 5.5.14
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Object to array cast results in duck typing
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 bug report at https://bugs.php.net/bug.php?id=67640&edit=1
--