Bug->Doc #75135 [Opn]: Elements of DirectoryIterator is the iterator itself

From: Date: Wed, 12 May 2021 13:15:09 +0000
Subject: Bug->Doc #75135 [Opn]: Elements of DirectoryIterator is the iterator itself
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18759@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75135&edit=1

 ID:                 75135
 Updated by:         cmb@php.net
 Reported by:        morozov at tut dot by
 Summary:            Elements of DirectoryIterator is the iterator itself
 Status:             Open
-Type:               Bug
+Type:               Documentation Problem
 Package:            SPL related
 Operating System:   Linux
 PHP Version:        7.1.8
 Block user comment: N
 Private report:     N

 New Comment:

In my opinion, DirectoryIterator is badly designed; it should not
extend SplFileInfo, but rather yield SplFileInfo instances on
iteration.  However, this is not something that can fixed at this
point in time, or at the very least that would require careful
consideration and the RFC process[1].

The behavior should, however, be explicitly mentioned in the docs.

[1] <https://wiki.php.net/rfc/howto>


Previous Comments:
------------------------------------------------------------------------
[2017-08-29 17:38:13] morozov at tut dot by

Description:
------------
When iterating over a DirectoryIterator, each element is the iterator itself representing the
current file. It makes it impossible to convert the iterator to array, as all its elements will be
references to the same object (the iterator).

See https://3v4l.org/P0H06.

Test script:
---------------
$it = new DirectoryIterator('/tmp');
$array = iterator_to_array($it);

foreach ($array as $fileInfo) {
    var_dump($fileInfo->getRealPath());
}

/*

Outputs:

bool(false)
bool(false)

*/

Expected result:
----------------
The expected result is to see real paths of the files under the /tmp directory. It works if the
iterator is iterated directly, without being converted to an array first.

Actual result:
--------------
The actual result is all real paths are FALSE since after having been converted to an array, the
directory iterator is in an invalid state.


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



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


Thread (3 messages)

« previous php.doc.bugs (#18759) next »