note 123789 added to directoryiterator.construct
| From: | cmanley at example dot com | Date: | Mon, 22 Apr 2019 07:10:58 +0000 |
| Subject: | note 123789 added to directoryiterator.construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210652@lists.php.net to get a copy of this message | ||
Beware that DirectoryIterator returns a reference to itself with each iteration instead of a unique
new SplFileInfo object as one may first assume.
This is important to know if you want to push the results of an iteration into an array for later
processing.
If you do that, then all entries in the array will be the same DirectoryIterator object with the
same internal state (most likely invalid - see the valid() method), which is probably not what you
want.
So if you do want to store the iteration results in an array, then you must convert them to either
plain strings or new SplFileInfo objects first.
$todo = [];
foreach (new DirectoryIterator('/some/dir') as $entry) {
if (!$entry->isFile()) {
continue;
}
#$todo []= $entry; # Don't do this!
$todo []= new SplFileInfo($entry->getFilename()); # Do this!
#$todo []= $entry->getFilename(); # or do this if you're only interested in the file name.
}
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 2001:980:38e1:1:1c05:c4cc:3de0:6415)
----
Manual Page -- http://php.net/manual/en/directoryiterator.construct.php
Edit -- https://master.php.net/note/edit/123789
Del: integrated -- https://master.php.net/note/delete/123789/integrated
Del: useless -- https://master.php.net/note/delete/123789/useless
Del: bad code -- https://master.php.net/note/delete/123789/bad+code
Del: spam -- https://master.php.net/note/delete/123789/spam
Del: non-english -- https://master.php.net/note/delete/123789/non-english
Del: in docs -- https://master.php.net/note/delete/123789/in+docs
Del: other reasons-- https://master.php.net/note/delete/123789
Reject -- https://master.php.net/note/reject/123789
Search -- https://master.php.net/manage/user-notes.php