|  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
39 + 45 = ?
Subscribe to this entry?

 [2018-05-18 07:18 UTC] biser dot antonov at gmail dot com
When providing a custom iterator to an ArrayObject, the iterator's constructor is not called on foreach.

Tested in the official php docker container:

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 


Add a Patch

Pull Requests

Add a Pull Request


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

Anyhow, given the long standing behavior, and that it is
apparently a design decision, I'm switching to documentation
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 19:01:28 2024 UTC