|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78223 DirectoryIterator::current returns itself, also other classes
Submitted: 2019-06-28 02:01 UTC Modified: 2021-05-12 13:16 UTC
From: zerkms at zerkms dot ru Assigned: cmb (profile)
Status: Duplicate Package: SPL related
PHP Version: 7.3.6 OS: linux
Private report: No CVE-ID: None
 [2019-06-28 02:01 UTC] zerkms at zerkms dot ru
It's reproducible on all php >= 7 versions (including 7.4-rc)

I probably am missing something, but I cannot find it documented anywhere, hence the behaviour surprises me.

Test script:

$files = iterator_to_array(new DirectoryIterator(__DIR__));

foreach (new DirectoryIterator(__DIR__) as $f) {

Expected result:
string(4) "/app"
string(4) "/app"

Actual result:
string(4) "/app"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-28 02:12 UTC]
-Summary: DirectoryIterator entries invalidated after its destruction +Summary: DirectoryIterator::current returns itself, also other classes
 [2019-06-28 02:12 UTC]
Dump out $files and you'll see all entries in it are the same object: the DirectoryIterator itself. When iterator_to_array finishes the DirectoryIterator will be at the end of the list and so invalid, which then means all the (duplicate) values in the array are also invalid.

It's a quirk of how DirectoryIterator and some others work. I would love to see this fixed, but that may not be feasible...
 [2021-05-12 13:16 UTC]
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2021-05-12 13:16 UTC]
I'm closing this as duplicate of bug #75135.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Thu Mar 23 05:03:38 2023 UTC