php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80380 Cannot access numeric property in ArrayObject
Submitted: 2020-11-19 09:33 UTC Modified: 2020-11-23 18:26 UTC
From: anton at rudnikov dot net Assigned:
Status: Verified Package: SPL related
PHP Version: 7.4.12 OS: any
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: anton at rudnikov dot net
New email:
PHP Version: OS:

 

 [2020-11-19 09:33 UTC] anton at rudnikov dot net
Description:
------------
I pass an object to ArrayObject constructor, and trying to access its properties. I can access string properties, but I can't access numeric one

Test script:
---------------
<?php

$object = new stdClass;
$object->prop = 'test1';
$object->{'1'} = 'test2';

$arrayObject = new ArrayObject($object);
echo $arrayObject['prop']; // test1              
echo $arrayObject['1']; // Undefined index

$arrayObject = new ArrayObject($object, ArrayObject::ARRAY_AS_PROPS);
echo $arrayObject->prop; // test1              
echo $arrayObject->{'1'}; // Undefined index

Expected result:
----------------
test1
test2
test1
test2

Actual result:
--------------
test1
Notice: Undefined index: 1 in /in/9UdHs on line 9
test1
Notice: Undefined index: 1 in /in/9UdHs on line 13

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-19 09:42 UTC] anton at rudnikov dot net
-Package: *Programming Data Structures +Package: SPL related
 [2020-11-19 09:42 UTC] anton at rudnikov dot net
Changed package
 [2020-11-19 11:34 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2020-11-19 11:34 UTC] cmb@php.net
Given that numeric properties are accessible after array
conversion as of PHP 7.2.0, it seems wrong that this is not the
case with ArrayObjects.
 [2020-11-19 12:32 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #80380: Cannot access numeric property in ArrayObject
On GitHub:  https://github.com/php/php-src/pull/6436
Patch:      https://github.com/php/php-src/pull/6436.patch
 [2020-11-20 08:17 UTC] anton at rudnikov dot net
There's also interesting behaviour when I try to get array copy from ArrayObject using method getArrayCopy.

Test script:
---------------
<?php

$object = new stdClass();
$object->prop = 'test1';
$object->{'1'} = 'test2';

$arrayObject = new ArrayObject($object);
$arrayCopy = $arrayObject->getArrayCopy();
var_dump($arrayCopy[1], $arrayCopy['1']); // can't access numeric property on array copy

$array = (array) $object;
var_dump($array[1], $array['1']); // but it works if I just cast stdClass to array

var_dump($arrayCopy, $array); // let's see differences between arrays

Actual result:
--------------
Notice: Undefined offset: 1 in /tmp/preview on line 8

Notice: Undefined offset: 1 in /tmp/preview on line 8
NULL
NULL
string(4) "test"
string(4) "test"
array(1) {
  ["1"]=>
  string(4) "test"
}
array(1) {
  [1]=>
  string(4) "test"
}
 [2020-11-23 18:26 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2020-11-23 18:26 UTC] cmb@php.net
Yes, that is another instance of the same root cause, which is
that ArrayObject treats object properties ("proptables") as arrays
("symtables").  Yet another instance:

<?php
$object = new stdClass;
$object->{'1'} = 'test1';

$arrayObject = new ArrayObject($object);
$arrayObject['2'] = 'test2';

var_dump($object);
?>

We have just created an inaccessible numeric property on the
stdClass instance.

It seems to me that the fix is not trivial, and that it might
break some existing code.  As such, I don't think this should
be fixed for any stable PHP version, but rather documented.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun May 09 14:01:24 2021 UTC