php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76354 ArrayObject doesn't call the constructor of a custom iterator when foreach
Submitted: 2018-05-18 07:18 UTC Modified: 2021-09-27 15:25 UTC
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-27 15:25 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-09-27 15:25 UTC] cmb@php.net
Well, only constructors without any mandatory parameters could be
successfully called anyway, since there is no way to specify the
arguments.

Anyhow, given the long standing behavior, and that it is
apparently a design decision, I'm switching to documentation
problem.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Sep 28 14:03:36 2021 UTC