php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76355 ArrayObject allows foreach by reference
Submitted: 2018-05-18 07:40 UTC Modified: -
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: biser dot antonov at gmail dot com Assigned:
Status: Open Package: SPL related
PHP Version: 7.2.5 OS: Debian
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: biser dot antonov at gmail dot com
New email:
PHP Version: OS:

 

 [2018-05-18 07:40 UTC] biser dot antonov at gmail dot com
Description:
------------
I don't know if it's bug but the ArrayObject allows foreach by reference. 

If however I create it with a custom iterator which overrides the current() method a fatal error is produced: 

Fatal error: An iterator cannot be used with foreach by reference

Links with both cases can be found here:
https://3v4l.org/aBKtd
https://3v4l.org/00Z3F


Test script:
---------------
$a = new ArrayObject([1, 2, 3]);
foreach ($a as $key => &$value) {
    $value += 10;
}
unset($value);

var_dump($a);

Expected result:
----------------
Fatal error: An iterator cannot be used with foreach by reference

Actual result:
--------------
object(ArrayObject)#1 (1) {
  ["storage":"ArrayObject":private]=>
  array(3) {
    [0]=>
    int(11)
    [1]=>
    int(12)
    [2]=>
    int(13)
  }
}


Patches

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC