php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76354 ArrayObject doesn't call the constructor of a custom iterator when foreach
Submitted: 2018-05-18 07:18 UTC Modified: -
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:18 UTC] biser dot antonov at gmail dot com
Description:
------------
When providing a custom iterator to an ArrayObject, the iterator's constructor is not called on foreach.

Tested in the official php docker container:

https://github.com/docker-library/php/blob/b045ba7c51ceed8a495beb8ea7274df48a3c70e1/7.2/stretch/cli/Dockerfile

Test script:
---------------
class I extends ArrayIterator
{
    public function __construct(array $array = array(), $flags = 0)
    {
        exit(__FILE__ . ':' . __LINE__);
    }
}

$a = new ArrayObject([1, 2, 3], 0, I::class);
foreach ($a as $key => $value) {
    // do something
}


Expected result:
----------------
I expected the script to call I::__construct() and exit

Actual result:
--------------
The script finished without interruption 

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