Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 704863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:59:21+00:00 2026-05-14T03:59:21+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:59:21+00:00Added an answer on May 14, 2026 at 3:59 am

    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);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.