Bug #78223 [Opn->Dup]: DirectoryIterator::current returns itself, also other classes

From: Date: Wed, 12 May 2021 13:16:11 +0000
Subject: Bug #78223 [Opn->Dup]: DirectoryIterator::current returns itself, also other classes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233818@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78223&edit=1

 ID:                 78223
 Updated by:         cmb@php.net
 Reported by:        zerkms at zerkms dot ru
 Summary:            DirectoryIterator::current returns itself, also
                     other classes
-Status:             Open
+Status:             Duplicate
 Type:               Bug
 Package:            SPL related
 Operating System:   linux
 PHP Version:        7.3.6
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

I'm closing this as duplicate of bug #75135.


Previous Comments:
------------------------------------------------------------------------
[2019-06-28 02:12:15] requinix@php.net

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

------------------------------------------------------------------------
[2019-06-28 02:01:53] zerkms at zerkms dot ru

Description:
------------
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:
---------------
<?php

$files = iterator_to_array(new DirectoryIterator(__DIR__));
var_dump($files[0]->getRealPath());

foreach (new DirectoryIterator(__DIR__) as $f) {
    var_dump($f->getRealPath());
    break;
}

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

Actual result:
--------------
bool(false)
string(4) "/app"


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78223&edit=1


Thread (3 messages)

« previous php.bugs (#233818) next »