|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-02-27 04:03 UTC] frozenfire@php.net
[2013-02-27 04:03 UTC] frozenfire@php.net
-Status: Open
+Status: Not a bug
[2013-04-02 15:30 UTC] maciej dot sz at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 15:00:02 2026 UTC |
Description: ------------ Since DirectoryIterator::current() returns itself there is many confusion while using it. It can't for instance be used as an argument of recursive method. Test script: --------------- foreach ( new DirectoryIterator(__DIR__) as $Current ) { var_dump($Di === $Current); } bool(true) bool(true) bool(true) ... Expected result: ---------------- In order to use DirectoryIterator object as a function parameter you MUST clone it: function iterateDirRecursive(DirectoryIterator $Di) { foreach ( $Di as $Item ) { $Current = clone $Item; // notice the clone here if ( $Current->isDir() && ! $Current->isDot() ) { iterateDirRecursive($Current); } } } iterateDirRecursive(new DirectoryIterator(__DIR__)); Actual result: -------------- Otherwise it will fail with "out of memory" or "maximum nesting level reached" depending on your PHP compilation: function iterateDirRecursive(DirectoryIterator $Di) { foreach ( $Di as $Item ) { $Current = $Item; // clone is missing if ( $Current->isDir() && ! $Current->isDot() ) { iterateDirRecursive($Current); } } } iterateDirRecursive(new DirectoryIterator(__DIR__));