将自定义 FileInfo 类设置为迭代器的正确方法

发布于 2024-08-27 18:51:53 字数 2345 浏览 9 评论 0原文

我试图通过 setInfoClass方法:

使用此方法设置一个自定义类,该类将在调用 getFileInfo 和 getPathInfo 时使用。传递给此方法的类名必须派生自 SplFileInfo。

我的类是这样的(简化示例):

class MyFileInfo extends SplFileInfo
{
    public $props = array(
        'foo' => '1',
        'bar' => '2'
    );
}

迭代器代码是这样的:

$rit = new RecursiveIteratorIterator(
           new RecursiveDirectoryIterator('/some/file/path/'),
           RecursiveIteratorIterator::SELF_FIRST);

由于 RecursiveDirectoryIterator 是通过 DirectoryIterator< 继承的/code>也是一个 SplFileInfo 对象,它提供了 setInfoClass 方法。它没有在手册中列出,但反射显示它在那里:

shell$ php --rc RecursiveDirectoryIterator
// ...
Method [ <internal:SPL, inherits SplFileInfo> public method setInfoClass ] {  
  - Parameters [1] {
    Parameter #0 [ <optional> $class_name ]
  }
}

到目前为止一切都很好,但是当迭代目录时

$rit->getInnerIterator()->setInfoClass('MyFileInfo');
foreach($rit as $file) {
    var_dump( $file );
}

我得到以下奇怪的结果

object(MyFileInfo)#4 (3) {
  ["props"]=>UNKNOWN:0
  ["pathName":"SplFileInfo":private]=>string(49) "/some/file/path/someFile.txt"
  ["fileName":"SplFileInfo":private]=>string(25) "someFile.txt"
}

因此,当 MyFileInfo 被拾取时,我无法访问它特性。如果我添加自定义方法,我可以很好地调用它们,但任何属性都是未知的。

如果我不将信息类设置为迭代器,而是设置为 SplFileInfo 对象(如手册中的示例所示),它将给出相同的未知结果:

foreach($rit as $file) {
    // $file is a SplFileInfo instance
    $file->setInfoClass('MyFileInfo');
    var_dump( $file->getFileInfo() );
}

但是,当我这样做时它会起作用

foreach($rit as $file) {
    $file = new MyFileInfo($file);
    var_dump( $file );
}

不幸的是,我的代码想要使用它有点复杂,并且堆叠了更多迭代器。像这样创建 MyFileInfo 类不是一个选项。

那么,有谁知道如何让它工作或者为什么 PHP 的行为如此奇怪?

谢谢。

I am trying to set a custom class to an Iterator through the setInfoClass method:

Use this method to set a custom class which will be used when getFileInfo and getPathInfo are called. The class name passed to this method must be derived from SplFileInfo.

My class is like this (simplified example):

class MyFileInfo extends SplFileInfo
{
    public $props = array(
        'foo' => '1',
        'bar' => '2'
    );
}

The iterator code is this:

$rit = new RecursiveIteratorIterator(
           new RecursiveDirectoryIterator('/some/file/path/'),
           RecursiveIteratorIterator::SELF_FIRST);

Since RecursiveDirectoryIterator is by inheritance through DirectoryIterator also an SplFileInfo object, it provides the setInfoClass method. It's not listed in the manual, but reflection shows it's there:

shell$ php --rc RecursiveDirectoryIterator
// ...
Method [ <internal:SPL, inherits SplFileInfo> public method setInfoClass ] {  
  - Parameters [1] {
    Parameter #0 [ <optional> $class_name ]
  }
}

All good up to here, but when iterating over the directory with

$rit->getInnerIterator()->setInfoClass('MyFileInfo');
foreach($rit as $file) {
    var_dump( $file );
}

I get the following weird result

object(MyFileInfo)#4 (3) {
  ["props"]=>UNKNOWN:0
  ["pathName":"SplFileInfo":private]=>string(49) "/some/file/path/someFile.txt"
  ["fileName":"SplFileInfo":private]=>string(25) "someFile.txt"
}

So while MyFileInfo is picked up, I cannot access it's properties. If I add custom methods, I can invoke them fine, but any properties are UNKNOWN.

If I don't set the info class to the iterator, but to the SplFileInfo object (like shown in the example in the manual), it will give the same UNKNOWN result:

foreach($rit as $file) {
    // $file is a SplFileInfo instance
    $file->setInfoClass('MyFileInfo');
    var_dump( $file->getFileInfo() );
}

However, it will work when I do

foreach($rit as $file) {
    $file = new MyFileInfo($file);
    var_dump( $file );
}

Unfortunately, the code I want to use this in is somewhat more complicated and stacks some more iterators. Creating the MyFileInfo class like this is not an option.

So, does anyone know how to get this working or why PHP behaves this weird?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜雨飘雪 2024-09-03 18:51:53

无法告诉你确切的原因,但它适用于

class MyFileInfo extends SplFileInfo
{
  public $props;

  public function __construct($filename) {
    $this->props = array(
      'foo' => '1',
      'bar' => '2'
    );

    parent::__construct($filename);
  }
}

Can't tell you exactly why but it works with

class MyFileInfo extends SplFileInfo
{
  public $props;

  public function __construct($filename) {
    $this->props = array(
      'foo' => '1',
      'bar' => '2'
    );

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