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

 ID:          51682
 Updated by:  fel...@php.net
 Reported by: david at grudl dot com
 Summary:     (array) casting and numeric keys
-Status:      Open
+Status:      Bogus
 Type:        Bug
 Package:     Arrays related
 PHP Version: Irrelevant

 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See bug #45959


Previous Comments:
------------------------------------------------------------------------
[2010-04-28 22:55:09] ptacek dot pavel at gmail dot com

Test 1: 

--------

class Test {

    protected $25twentyFive;

}



Expected result: Parse error.

Actual result: Parse error.



Test 2:

--------

$obj = new stdClass;

$obj->{25} = 'test assign as-integer';



$arr = (array) $obj;

$arr[25] = 'test assign into array, with integer';



var_dump($arr);



Expected result 2:

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

Parse error (since numeric variables are not compliant; when doing
$obj->25 i 

get an parse error)



Actual result 2:

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

array(2) {

  ["25"]=>

  string(22) "test assign as-integer"

  [25]=>

  string(36) "test assign into array, with integer"

}



Test 3:

--------

$obj = new stdClass;

$twentyFive = 25;

$obj->$twentyFive = 'test assing with integer';



$arr = (array) $obj;

$arr[$twentyFive] = 'test assign into array';



var_dump($arr);



Expected result 3:

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

array(1) {

  [25]=>

  string(22) "test assign into array"

}



OR notice.



Actual result 3:

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

array(2) {

  ["25"]=>

  string(24) "test assing with integer"

  [25]=>

  string(22) "test assign into array"

}



Proposed solution:

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

When doing:

    $twentyFive = 25;

    $obj->$twentyFive = 'some string';



php should raise a notice





When doing:

    $obj->{25} = 'some string';



php should raise an error.

------------------------------------------------------------------------
[2010-04-28 17:04:03] pavel dot lisa at centrum dot cz

I'd say there is no error in the way PHP handles this, as you are
accessing 

$obj->{'10'} by a string key ( '10' === "10" => true ). 



$obj = new stdClass;

$obj->{"10"} = 'as string';



$arr = (array) $obj;

$arr["10"] = 'as string again';



var_dump($arr);



would then produce something like



array(1) {

  ["10"]=>

  string(10) "as string again"

}

------------------------------------------------------------------------
[2010-04-28 15:30:25] david at grudl dot com

Description:
------------
Casting to (array) handles numeric keys wrong way:





Test script:
---------------
$obj = new stdClass;

$obj->{'10'} = 'as string';



$arr = (array) $obj;

$arr[10] = 'as integer';



var_dump($arr);



Expected result:
----------------
array(1) {

  [10]=>

  string(10) "as integer"

}





Actual result:
--------------
array(2) {

  ["10"]=>

  string(9) "as string"

  [10]=>

  string(10) "as integer"

}






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



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

Reply via email to