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
 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC