php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71208 Array/Object cast should not loose numeric property/element accessibility
Submitted: 2015-12-23 23:50 UTC Modified: 2016-09-24 01:12 UTC
From: ajf@php.net Assigned: ajf (profile)
Status: Duplicate Package: Scripting Engine problem
PHP Version: 7.0.1 OS: OS X 10.11 El Capitan
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ajf@php.net
New email:
PHP Version: OS:

 

 [2015-12-23 23:50 UTC] 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

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-23 23:51 UTC] ajf@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: ajf
 [2015-12-23 23:52 UTC] ajf@php.net
This has been broken since at least PHP 5.4.
 [2015-12-23 23:52 UTC] ajf@php.net
-Summary: Array/Object cast looses numeric property/element accessibility +Summary: Array/Object cast loses numeric property/element accessibility
 [2015-12-24 01:01 UTC] 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'});

?>
 [2016-08-08 12:17 UTC] cmb@php.net
-Summary: Array/Object cast loses numeric property/element accessibility +Summary: Array/Object cast should not loose numeric property/element accessibility
 [2016-08-08 12:17 UTC] 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:18 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request
 [2016-08-12 12:24 UTC] cmb@php.net
-Status: Assigned +Status: Duplicate
 [2016-08-12 12:24 UTC] cmb@php.net
Well, actually this is a duplicate of bug #66173.
 [2016-09-24 01:12 UTC] ajf@php.net
It's not a duplicate of bug #66173, it's a deliberate counterpart to it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 15:01:30 2024 UTC