Bug->Doc #76354 [Opn]: ArrayObject doesn't call the constructor of a custom iterator when foreach
| From: | cmb@php.net | Date: | Mon, 27 Sep 2021 15:25:56 +0000 |
| Subject: | Bug->Doc #76354 [Opn]: ArrayObject doesn't call the constructor of a custom iterator when foreach | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-19230@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76354&edit=1
ID: 76354
Updated by: cmb@php.net
Reported by: biser dot antonov at gmail dot com
Summary: ArrayObject doesn't call the constructor of a custom
iterator when foreach
Status: Open
-Type: Bug
+Type: Documentation Problem
Package: SPL related
Operating System: Debian
PHP Version: 7.2.5
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2018-05-18 07:18:36] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76354&edit=1