Doc #64292 [NEW]: Missing documentation details for return value of DirectoryIterator::current()
| From: | maciej dot sz at gmail dot com | Date: | Sun, 24 Feb 2013 15:19:15 +0000 |
| Subject: | Doc #64292 [NEW]: Missing documentation details for return value of DirectoryIterator::current() | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-9581@lists.php.net to get a copy of this message | ||
From: maciej dot sz at gmail dot com
Operating system: irrelevant
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Missing documentation details for return value of DirectoryIterator::current()
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__));
--
Edit bug report at https://bugs.php.net/bug.php?id=64292&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64292&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64292&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64292&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64292&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64292&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64292&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64292&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64292&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64292&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64292&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=64292&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=64292&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64292&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64292&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64292&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64292&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64292&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64292&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64292&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64292&r=mysqlcfg