Bug #54209 [Opn->Dup]: Inconsistent array index behaviour

From: Date: Fri, 11 Mar 2011 03:52:10 +0000
Subject: Bug #54209 [Opn->Dup]: Inconsistent array index behaviour
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-159386@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54209&edit=1

 ID:                 54209
 Updated by:         aharvey@php.net
 Reported by:        phristen at yahoo dot com
 Summary:            Inconsistent array index behaviour
-Status:             Open
+Status:             Duplicate
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

Duplicate of numerous bugs, including bug #45959. In short, this is
expected behaviour, and is unlikely to be changed, as doing so would
cause performance issues within PHP.


Previous Comments:
------------------------------------------------------------------------
[2011-03-09 22:25:57] phristen at yahoo dot com

Description:
------------
It is well known that PHP casts numeric string indexes to integers, and
it is normally impossible to force such indexes to remain strings. For
example, the script below will output "integer":

$test = array("123" => 1);
echo gettype(key($test)); // integer

However, it is possible to achieve this if we assign numeric string
property to an object (by using the ->{} syntax), and then cast it to
array. Not only will the index retain it's string type, but it will also
be impossible to access that index with [], which can't be correct
behaviour. See attached example.

Test script:
---------------
<?php
$test = new stdClass();
$test->{"123"} = 1;
$test = (array)$test;

$key = key($test);
echo gettype($key);
echo "\n";
echo $test[$key];
?>

Expected result:
----------------
integer
1

Actual result:
--------------
string
PHP Notice: Undefined index: 123 in test.php on line 9


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54209&edit=1


Thread (2 messages)

« previous php.bugs (#159386) next »