Req #71208 [Asn->Dup]: Array/Object cast should not loose numeric property/element accessibility
Edit report at https://bugs.php.net/bug.php?id=71208&edit=1
ID: 71208
Updated by: cmb@php.net
Reported by: ajf@php.net
Summary: Array/Object cast should not loose numeric
property/element accessibility
-Status: Assigned
+Status: Duplicate
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: OS X 10.11 El Capitan
PHP Version: 7.0.1
Assigned To: ajf
Block user comment: N
Private report: N
New Comment:
Well, actually this is a duplicate of bug #66173.
Previous Comments:
------------------------------------------------------------------------
[2016-08-08 12:17:56] cmb@php.net
For the record: this issue had already been reported as bug
#53838, which had been suspended, and as bug #61655, which has
been marked as not-a-bug, because of performance issues.
So that's probably something that would have to be discussed on
internals, if that hadn't already happened.
> This has been broken since at least PHP 5.4.
It behaves this way as of PHP 5.0.0, and didn't work earlier at
all, see <https://3v4l.org/55kiC>.
------------------------------------------------------------------------
[2016-08-08 12:13:14] cmb@php.net
Related To: Bug #70835
------------------------------------------------------------------------
[2015-12-24 01:01:42] ajf@php.net
Er, the test script has an off-by-one error. It should be:
<?php
$arr = [1, 2, 4];
$obj = (object)$arr;
var_dump($obj);
var_dump($obj->{0}, $obj->{1}, $obj->{2});
var_dump($obj->{'0'}, $obj->{'1'}, $obj->{'2'});
?>
------------------------------------------------------------------------
[2015-12-23 23:52:02] ajf@php.net
This has been broken since at least PHP 5.4.
------------------------------------------------------------------------
[2015-12-23 23:50:41] ajf@php.net
Description:
------------
If an array is casted to an object, its integer keys become inaccessible integer(!) key properties.
They should be normal string key properties instead.
Test script:
---------------
<?php
$arr = [1, 2, 4];
$obj = (object)$arr;
var_dump($obj);
var_dump($obj->{1}, $obj->{2}, $obj->{3});
var_dump($obj->{'1'}, $obj->{'2'}, $obj->{'3'});
?>
Expected result:
----------------
object(stdClass)#1 (3) {
["0"]=>
int(1)
["1"]=>
int(2)
["2"]=>
int(4)
}
int(1)
int(2)
int(4)
int(1)
int(2)
int(4)
Actual result:
--------------
object(stdClass)#1 (3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(4)
}
Notice: Undefined property: stdClass::$1 in - on line 7
Notice: Undefined property: stdClass::$2 in - on line 7
Notice: Undefined property: stdClass::$3 in - on line 7
NULL
NULL
NULL
Notice: Undefined property: stdClass::$1 in - on line 8
Notice: Undefined property: stdClass::$2 in - on line 8
Notice: Undefined property: stdClass::$3 in - on line 8
NULL
NULL
NULL
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71208&edit=1
Thread (7 messages)