DirectoryIterator和FileSystemIterator之间的区别 [英] Difference between DirectoryIterator and FileSystemIterator
问题描述
PHP 5引入了 DirectoryIterator ,PHP 5.3引入了FileSystemIterator .
PHP 5 introduced DirectoryIterator, and PHP 5.3 introduced FileSystemIterator.
FileSystemIterator
扩展了DirectoryIterator
,但是文档未说明其带来的额外功能.
FileSystemIterator
extends DirectoryIterator
, but the documentation fails to say what extra features it brings.
您能说出DirectoryIterator
和FileSystemIterator
之间的区别吗?
Can you tell the difference between DirectoryIterator
and FileSystemIterator
?
推荐答案
这超出了我的头脑,我陷入了PHP 5.3之前的变化,而这些变化将在5.3和更高版本中进行更改,关于SPL(StandardPHPLibrary)和将要移至(可怕的)PECL扩展的内容.
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.
自5.3以来发生的主要变化是SPL成为无法再禁用的扩展,请参见更改日志
The major thing that changed since 5.3, was that the SPL became an extension that could not be disabled anymore, see the changelog of 5.3 noting that
- 将SPL添加到了无法禁用的标准扩展列表中. (马库斯)
- Added SPL to list of standard extensions that cannot be disabled. (Marcus)
因此,所有喜欢的类(如DirectoryIterator或SPLDoublyLinkedList)现在都是PHP 5.3附带的类的修订套件.
so all the fancy classes like DirectoryIterator or SPLDoublyLinkedList were now a fix suite of classes that came with PHP 5.3.
正在进行很多讨论,DirectoryIterator在遍历文件/目录以及从不足够匿名到正在使用的文件系统的行为方面仍然非常笨拙.因为取决于文件系统(Windows NTFS/* nix EXTx),迭代器将返回的结果与另一个不同,在默认情况下,*nix
环境始终默认将点和双点目录(.
和..
)视为有效目录.然后可以使用isDot()
方法在循环中过滤这些点目录.
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 *nix
environments 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.
$it = new DirectoryIterator(__DIR__);
foreach ($it as $fileinfo) {
if (!$fileinfo->isDot())
var_dump($fileinfo->getFilename());
}
因此FilesystemIterator
成为PHP 5.3中的新父类,在其发布之前为DirectoryIterator
(其中FilesystemIterator
扩展了DirectoryIterator
以默认实现此可互换行为) .这样,行为或结果FilesystemIterator
就会等于所有不同的文件系统,并且可以互换而无需循环中的任何开销
So FilesystemIterator
became the new parent class in PHP 5.3, which prior to its release was the DirectoryIterator
(where FilesystemIterator
extends DirectoryIterator
to implement this interchangeable behaviour by default). The behaviour, or result the FilesystemIterator
produced, would then be equal to all different filesystems and interchangeable without the need of any overhead in the loop
$it = new FilesystemIterator(__DIR__);
foreach ($it as $fileinfo) {
echo $fileinfo->getFilename() . "\n";
}
这是一个很好的问题,为什么他们没有更新文档以引起用户注意FilesystemIterator
实际上是在DirectoryIterator
之前.
It's a good question why they didn't update the documentation for noticing the user on the fact that actually the FilesystemIterator
preceded the DirectoryIterator
.
这篇关于DirectoryIterator和FileSystemIterator之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!