Encountering recursion during comparison now results in an Error exception instead of a [E_ERROR](https://www.php.net/manual/en/errorfunc.constants.php#constant.e-error) fatal error.
I need some more information on the idea of Recursion during comparison:
I have written a recursive function that walks the file system and can be programed to respond to files and directories.
I its simplest form it just produces a file system mapping. But it can be used to delete, rename, and other operations.
Does recursion during comparison apply to this? (examples possibly?)
In truth I have a working function that does not normally produce E_ERROR. So I presume my question is partially answered.
So, what I get from this is that what is troublesome is the possibility of an endless loop?
When I wrote my recursive file system walking function every time it enters a directory
it bypasses . (self) and … (parent dir) listings via a switch statement…
Note: the use of $_ in every variable name is my own practice. It helps me visually to
spot variable references in code.
$_list = scandir($_targetDir);
for( $_i = 0; $_i < count($_list); $_i++)
{
switch($_list[$_i])
{
case ‘.’:
case ‘…’:
// add other case labels representing file names that should be bypassed
break;
default:
// process every other item in the list
// recursion comes when a directory is encountered and the loop enters and begins the next level.
// the looping ends when the end of a directory listing is reached and all of the included directories have been processed
// it pops back to a higher level and continues there. Eventually the whole file tree has been processed.
break;
}
}
Didn’t know or forgot this. After verification and if I’m correct, recursion during comparison appears when PHP compares 2 objects. Infinite recursion can appear when one of the objects compared references himself.
Example :
class RecursiveObject {
public $self;
public function __construct() {
$this->self = $this;
}
}
$obj1 = new RecursiveObject();
$obj2 = new RecursiveObject();
try {
$result = $obj1 == $obj2;
} catch (Error $e) {
echo "Une erreur a été capturée : " . $e->getMessage();
}
Which results in PHP 8.3:
PHP Fatal error: Nesting level too deep - recursive dependency? in /var/www/html/api-spotify/public/test.php on line 14
PHP Stack trace:
PHP 1. {main}() /var/www/html/api-spotify/public/test.php:0
And in PHP 8.4 :
Une erreur a été capturée : Nesting level too deep - recursive dependency?
Encountering recursion during comparison now results in an Error exception instead of a [E_ERROR](https://www.php.net/manual/en/errorfunc.constants.php#constant.e-error) fatal error.
I need some more information on the idea of Recursion during comparison:
I have written a recursive function that walks the file system and can be programed to respond to files and directories.
I its simplest form it just produces a file system mapping. But it can be used to delete, rename, and other operations.
Does recursion during comparison apply to this? (examples possibly?)
In truth I have a working function that does not normally produce E_ERROR. So I presume my question is partially answered.
Didn’t know or forgot this. After verification and if I’m correct, recursion during comparison appears when PHP compares 2 objects. Infinite recursion can appear when one of the objects compared references himself.
Example :
class RecursiveObject {
public $self;
public function __construct() {
$this->self = $this;
}
}
$obj1 = new RecursiveObject();
$obj2 = new RecursiveObject();
try {
$result = $obj1 == $obj2;
} catch (Error $e) {
echo "Une erreur a été capturée : " . $e->getMessage();
}
Which results in PHP 8.3:
PHP Fatal error: Nesting level too deep - recursive dependency? in /var/www/html/api-spotify/public/test.php on line 14
PHP Stack trace:
PHP 1. {main}() /var/www/html/api-spotify/public/test.php:0
And in PHP 8.4 :
Une erreur a été capturée : Nesting level too deep - recursive dependency?
Encountering recursion during comparison now results in an Error exception instead of a [E_ERROR](https://www.php.net/manual/en/errorfunc.constants.php#constant.e-error) fatal error.
I need some more information on the idea of Recursion during comparison:
I have written a recursive function that walks the file system and can be programed to respond to files and directories.
I its simplest form it just produces a file system mapping. But it can be used to delete, rename, and other operations.
Does recursion during comparison apply to this? (examples possibly?)
In truth I have a working function that does not normally produce E_ERROR. So I presume my question is partially answered.
So, what I get from this is that what is troublesome is the possibility of an endless loop?
When I wrote my recursive file system walking function every time it enters a directory
it bypasses . (self) and .. (parent dir) listings via a switch statement...
Note: the use of $_ in every variable name is my own practice. It helps me visually to
spot variable references in code.
$_list = scandir($_targetDir);
for( $_i = 0; $_i < count($_list); $_i++)
{
switch($_list[$_i])
{
case '.':
case '..':
// add other case labels representing file names that should be bypassed
break;
default:
// process every other item in the list
// recursion comes when a directory is encountered and the loop enters and begins the next level.
// the looping ends when the end of a directory listing is reached and all of the included directories have been processed
// it pops back to a higher level and continues there. Eventually the whole file tree has been processed.
break;
}
}
Note: It's unclear to me if you've actually encountered this problem with your code, or you're trying to work out when it might occur.
From a quick grep of the PHP source code[0], I believe the "Nesting level too deep" error does not refer to recursive function calls in the manner described above (which would likely result in either a SEGFAULT or an error referencing zend.max_allowed_stack_size such as in [1]). It specifically occurs when comparing arrays or objects in a manner that would result in an infinite loop due to self-references. See the .phpt results in the linked search for examples.
If you're encountering the "Nesting level too deep" error, something else is going on in your code other than a straight-forward recursive function call.
If objects aren't involved (where properties may directly or indirectly self-reference the object), explicit references (eg. `&$var`) almost certainly are.
Aside: When dealing with (Linux) filesystems, it may be possible to encounter infinite loops even when ignoring the `.` and `..` special entries if symlinks are involved. If you're sure symlinks aren't involved (and specifically a symlink crafted to refer back to a parent directory), you'll likely never encounter this issue. The other solution is to limit the search depth - eg. never search more than 5 directory levels deep. (This situation would result in a stack overflow / stack size limit being reached as mentioned above, and not "Nesting level too deep")