php DirectoryIterator 和 FileSystemIterator 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12532064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between DirectoryIterator and FileSystemIterator
提问by Benjamin
PHP 5 introduced DirectoryIterator, and PHP 5.3 introduced FileSystemIterator.
PHP 5 引入了DirectoryIterator,PHP 5.3 引入了FileSystemIterator。
FileSystemIteratorextends DirectoryIterator, but the documentation fails to say what extra features it brings.
FileSystemIteratorextends DirectoryIterator,但文档没有说明它带来了哪些额外的功能。
Can you tell the difference between DirectoryIteratorand FileSystemIterator?
你能告诉之间的区别DirectoryIterator和FileSystemIterator?
回答by dbf
This goes out of the top of my head, where I sort of got caught in the changes prior to PHP 5.3 that were going to change in 5.3 and later, concerning the SPL (StandardPHPLibrary) and stuff that were going to be moved to the (horrible) PECL extensions.
这超出了我的脑海,我有点陷入 PHP 5.3 之前的更改中,这些更改将在 5.3 及更高版本中更改,涉及 SPL (StandardPHPLibrary) 和将要移动到 (可怕)PECL 扩展。
The major thing that changed since 5.3, was that the SPL became an extension that could not be disabled anymore, see the changelogof 5.3 noting that
自 5.3 以来发生的主要变化是,SPL 成为无法再禁用的扩展,请参阅 5.3 的变更日志,指出
- Added SPL to list of standard extensions that cannot be disabled. (Marcus)
- 将 SPL 添加到无法禁用的标准扩展列表中。(马库斯)
so all the fancy classes like DirectoryIterator or SPLDoublyLinkedList were now a fix suite of classes that came with PHP 5.3.
所以所有像 DirectoryIterator 或 SPLDoublyLinkedList 这样的花哨类现在都是 PHP 5.3 附带的修复类套件。
There were a lot of discussions going on that the DirectoryIterator was still very clumsy in iterating over files/directories and from behaviour not anonymous enough to the filesystem being used. Because depending on the filesystem (Windows NTFS / *nix EXTx) the results the iterator would return were different from another, where *nixenvironments per default always resulted the dot and double dot directories (.and ..) as valid directories. These dot directories could then be filtered in the loop by using the isDot()method.
有很多讨论表明 DirectoryIterator 在迭代文件/目录以及从不够匿名的行为到正在使用的文件系统方面仍然非常笨拙。因为取决于文件系统(Windows NTFS / *nix EXTx),迭代器返回的结果与另一个不同,其中*nix默认环境总是将点和双点目录(.和..)作为有效目录。然后可以使用该isDot()方法在循环中过滤这些点目录。
$it = new DirectoryIterator(__DIR__);
foreach ($it as $fileinfo) {
if (!$fileinfo->isDot())
var_dump($fileinfo->getFilename());
}
So FilesystemIteratorbecame the new parent class in PHP 5.3, which prior to its release was the DirectoryIterator(where FilesystemIteratorextends DirectoryIteratorto implement this interchangeable behaviour by default). The behaviour, or result the FilesystemIteratorproduced, would then be equal to all different filesystems and interchangeable without the need of any overhead in the loop
因此FilesystemIterator成为 PHP 5.3 中的新父类,在其发布之前是DirectoryIterator(默认情况下FilesystemIterator扩展DirectoryIterator以实现此可互换行为)。行为或产生的结果FilesystemIterator将等于所有不同的文件系统并且可以互换,而无需循环中的任何开销
$it = new FilesystemIterator(__DIR__);
foreach ($it as $fileinfo) {
echo $fileinfo->getFilename() . "\n";
}
It's a good question why they didn't update the documentation for noticing the user on the fact that actually the FilesystemIteratorpreceded the DirectoryIterator.
这是一个很好的问题,为什么他们没有更新文档以通知用户实际上FilesystemIterator在DirectoryIterator.
回答by Baba
DirectoryIteratoris an extension of SplFileInfo
DirectoryIterator是的延伸 SplFileInfo
while
尽管
FilesystemIteratoris an extension of DirectoryIterator
FilesystemIterator是的延伸 DirectoryIterator
and the both implements
和两者都实现
Iterator , Traversable , SeekableIterator
Iterator , Traversable , SeekableIterator
FilesystemIteratorhas flags which affects its behaviors when working which files which can be very useful such as FOLLOW_SYMLINKS , SKIP_DOTS etcand this makes most of its difference.
FilesystemIterator有一些标志会影响它在处理哪些文件时的行为,这些文件非常有用,例如FOLLOW_SYMLINKS , SKIP_DOTS etc这使得它的大部分不同。
You can see full flags at FilesystemIterator predefined constants
您可以在FilesystemIterator 预定义常量中看到完整的标志
Example
例子
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ( $iterator as $fileinfo ) {
var_dump($fileinfo->current()); // would return object(DirectoryIterator)
}
Example 2
示例 2
$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::CURRENT_AS_PATHNAME);
foreach ( $iterator as $fileinfo ) {
var_dump($iterator->current()) . "\n"; // Would return full path eg /www/examples/example.php
}

