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:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.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 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: 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

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Apr 11 22:01:23 2021 UTC