|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65600 SplFileObject->next() not move next without current()
Submitted: 2013-09-01 10:09 UTC Modified: 2022-05-16 11:51 UTC
From: kentaro at ranvis dot com Assigned: cmb (profile)
Status: Duplicate Package: SPL related
PHP Version: 5.5.3 OS: *
Private report: No CVE-ID: None
 [2013-09-01 10:09 UTC] kentaro at ranvis dot com
SplFileObject->next() doesn't move to the next line unless ->current() is called in advance.

Test script:
$f = new SplFileObject('php://memory', 'r+');

$f->fwrite("line 1\nline 2\nline 3");
assert('$f->current() === "line 1\n"');
assert('$f->current() === "line 2\n"');
var_dump($f->ftell()); // 14
assert('$f->current() === "line 3"');

var_dump($f->ftell()); // 0
assert('$f->current() === "line 3"'); // fails


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-01 11:49 UTC]
-Status: Open +Status: Verified -Operating System: Windows +Operating System: *
 [2013-09-01 11:49 UTC]
Using the READ_AHEAD flag will enable the behavior you're expecting. I don't know 
if that requirement is intentional or necessary.
 [2013-09-01 14:46 UTC] kentaro at ranvis dot com
Thank you. It worked as expected with setFlags(SplFileObject::READ_AHEAD) before rewind().
I'll wait for further comment to see if this is a requirement (it seems to be.)
 [2016-07-01 15:48 UTC]

    $f = new SplFileObject('php://temp', 'w+');
    $f->fwrite("line 1\nline 2\nline 3");

While key() says we're at line 3, current() returns the first
line. That doesn't make sense; either key() should return 1, or
current() should return the third line.
 [2016-07-01 16:06 UTC]
Forgot: see <>.
 [2022-05-16 11:51 UTC]
-Status: Verified +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2022-05-16 11:51 UTC]
I'm closing this in favor of <>.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Mar 29 04:03:53 2023 UTC