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

From: Date: Thu, 19 Aug 2021 14:31:37 +0000
Subject: Doc #75135 [Opn->Ver]: Elements of DirectoryIterator is the iterator itself
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19089@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
+Status:             Verified
 Type:               Documentation Problem
 Package:            SPL related
 Operating System:   Linux
 PHP Version:        7.1.8
 Block user comment: N
 Private report:     N

 New Comment:

FilesystemIterator extends DirectoryIterator and has the very same
issue, namely that it iterates over itself.


Previous Comments:
------------------------------------------------------------------------
[2021-05-12 15:09:20] phpbugs at notanemail dot org

> In my opinion, DirectoryIterator is badly designed; it should not
> extend SplFileInfo, but rather yield SplFileInfo instances on
> iteration. 

FilesystemIterator was introduced, in PHP 5.3.0, whose default behaviour is to use SplFileInfo
instances as the "current" values.

DirectoryIterator should have been deprecated at that time. No-one should be using
DirectoryIterator: rather FilesystemIterator (or RecursiveDirectoryIterator, which extends
FilesystemIterator).

------------------------------------------------------------------------
[2021-05-12 13:16:11] cmb@php.net

Related To: Bug #78223

------------------------------------------------------------------------
[2021-05-12 13:15:09] cmb@php.net

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>

------------------------------------------------------------------------
[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 (#19089) next »