php.net |  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: 2016-07-01 16:06 UTC
From: kentaro at ranvis dot com Assigned:
Status: Verified Package: SPL related
PHP Version: 5.5.3 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-09-01 10:09 UTC] kentaro at ranvis dot com
Description:
------------
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");
$f->rewind();
assert('$f->current() === "line 1\n"');
$f->next();
assert('$f->current() === "line 2\n"');
$f->next();
var_dump($f->ftell()); // 14
assert('$f->current() === "line 3"');

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


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-01 11:49 UTC] requinix@php.net
-Status: Open +Status: Verified -Operating System: Windows +Operating System: *
 [2013-09-01 11:49 UTC] requinix@php.net
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] cmb@php.net
Consider:

    <?php
    $f = new SplFileObject('php://temp', 'w+');
    $f->fwrite("line 1\nline 2\nline 3");
    $f->rewind();
    $f->next();
    $f->next();
    $f->next();
    var_dump($f->key());
    var_dump($f->current());

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] cmb@php.net
Forgot: see <https://3v4l.org/qM88J>.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC