PHP 中的 DirectoryIterator+FilterIterator 行为不一致?

发布于 2024-08-18 05:58:19 字数 1256 浏览 7 评论 0原文

我编写了一个使用来自标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-08-25 05:58:19

看来这个其实还可以啊!在看似空的 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!

停顿的约定 2024-08-25 05:58:19

来自 PHP.net:

  • 注意:从 PHP 5.3.0 开始,此扩展无法再被禁用,因此始终可用。

可能您的 PHP 5.2.5 配置在某种程度上不完整/错误?您是否将错误日志级别设置为显示警告?如果没有,请调整它并再次运行您的代码以查看它是否抛出任何警告......

From PHP.net:

  • Note: As of PHP 5.3.0 this extension can no longer be disabled and is therefore always available.

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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文