PHP 中的 DirectoryIterator+FilterIterator 行为不一致?
我编写了一个使用来自标准 PHP 库 (SPL) 的 FilterIterator 类的脚本,并且在 PHP 5.x 版本中我得到了不同的行为:( 这里是 Accept() 函数:
public function accept()
{
$current = $this->current();
print_r($current);
return true;
}
下面是 PHP 5.3.1 的输出:
DirectoryIterator Object
(
[pathName:SplFileInfo:private] => /Users/alex/Sites/dev/php/fscms/content/.
[fileName:SplFileInfo:private] => .
[glob:DirectoryIterator:private] =>
[subPathName:RecursiveDirectoryIterator:private] =>
)
DirectoryIterator Object
(
[pathName:SplFileInfo:private] => /Users/alex/Sites/dev/php/fscms/content/..
[fileName:SplFileInfo:private] => ..
[glob:DirectoryIterator:private] =>
[subPathName:RecursiveDirectoryIterator:private] =>
)
DirectoryIterator Object
(
[pathName:SplFileInfo:private] => /Users/alex/Sites/dev/php/fscms/content/.DS_Store
[fileName:SplFileInfo:private] => .DS_Store
[glob:DirectoryIterator:private] =>
[subPathName:RecursiveDirectoryIterator:private] =>
)
这是我在 PHP 5.2.5 下使用相同代码得到的结果
DirectoryIterator Object
(
)
DirectoryIterator Object
(
)
DirectoryIterator Object
(
)
后者对所有文件都有权限 755 。
I wrote a script that is using the FilterIterator class that comes from the Standard PHP Library (SPL) and I get different behabours accross PHP 5.x versions :( Here the accept() function:
public function accept()
{
$current = $this->current();
print_r($current);
return true;
}
and heres its output for PHP 5.3.1:
DirectoryIterator Object
(
[pathName:SplFileInfo:private] => /Users/alex/Sites/dev/php/fscms/content/.
[fileName:SplFileInfo:private] => .
[glob:DirectoryIterator:private] =>
[subPathName:RecursiveDirectoryIterator:private] =>
)
DirectoryIterator Object
(
[pathName:SplFileInfo:private] => /Users/alex/Sites/dev/php/fscms/content/..
[fileName:SplFileInfo:private] => ..
[glob:DirectoryIterator:private] =>
[subPathName:RecursiveDirectoryIterator:private] =>
)
DirectoryIterator Object
(
[pathName:SplFileInfo:private] => /Users/alex/Sites/dev/php/fscms/content/.DS_Store
[fileName:SplFileInfo:private] => .DS_Store
[glob:DirectoryIterator:private] =>
[subPathName:RecursiveDirectoryIterator:private] =>
)
and heres what I get with the same code under PHP 5.2.5
DirectoryIterator Object
(
)
DirectoryIterator Object
(
)
DirectoryIterator Object
(
)
The latter has permission 755 on all files. Whats up with dat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来这个其实还可以啊!在看似空的 DirectoryIterator 对象上调用方法是有效的。我遇到的问题在其他地方,并且与克隆这些对象有关。似乎 PHP 5.3 克隆得很深,而 5.2 则不然,所以所有引用都会在每次迭代时被杀死(或类似的事情)。这是一个丑陋的、没有记录的混乱:PHP 很糟糕!
Seems like this is actually OK! Calling methods on the seemingly empty DirectoryIterator objects works. The issues I was having were elsewhere and had to do with cloning these objects. Seems PHP 5.3 clones deep while 5.2 don't so all the references get killed on each iteration (or something like that). Its an ugly, un-documented mess: PHP sucks!
来自 PHP.net:
可能您的 PHP 5.2.5 配置在某种程度上不完整/错误?您是否将错误日志级别设置为显示警告?如果没有,请调整它并再次运行您的代码以查看它是否抛出任何警告......
From PHP.net:
Possibly your PHP 5.2.5 configuration is in some way incomplete/wrong? Do you have your error log leve set to display warnings? If not, adjust it and run your code again to see if its throwing any warnings ...