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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 05:01:29 2024 UTC