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
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:
MUST BE VALID
Solve the problem:
29 - 17 = ?
Subscribe to this entry?

 
 [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: Fri Sep 17 19:03:37 2021 UTC