Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does " *RECURSION* " in print_r output mean?

Tags:

oop

php

recursion

I'm using this recursive code to read all directories inside another directory, and store them within the parent directory.

protected function readDirs($parent)
    {       
        $currentDir = $parent->source();
        $items = scandir($currentDir);

        foreach ($items as $itemName)
        {
            if (Dir::isIgnorable($itemName) )
                continue;

            $itemPath = $currentDir.SLASH.$itemName;
            if (! is_dir($itemPath) )
                continue;

            $item = new ChangeItem(TYPE_DIR);            
            $item->parent($parent)->source($itemPath);

            $parent->children[ $itemName ] = $item;

            $this->readDirs($item);
        }
    }

After this is done, if I do a print_r() on the global Object which is storing everything else, for some of the items it says:

[parent:protected] => ChangeItem Object
 *RECURSION*

What does that mean? Will I be able to access the parent object or not?

like image 943
Ali Avatar asked Apr 26 '11 04:04

Ali


2 Answers

It means that the property is a reference to an object that has already been visited by print_r. print_r detects this and doesn't continue down that path; otherwise, the resulting output would be infinitely long.

In the context of your program, as scandir also returns references to the current and parent directories (named . and .., respectively), following them would lead to recursion. Following symbolic links may also cause recursion.

like image 97
Delan Azabani Avatar answered Sep 23 '22 07:09

Delan Azabani


scandir returns the . entry, which represents the current directory. You then go to store this directory inside its parent (itself). Thus, recursion.

I suggest ignoring . and ...

The "RECURSION" message you got means the data structure cannot be printed in its entirety because it would be infinite.

like image 27
Borealid Avatar answered Sep 24 '22 07:09

Borealid