Skip to content

SplFileObject: next() moves to inexistent indexes #8564

Description

@gabrielpl

Description

The following code:

<?php

declare(strict_types=1);

const LINES = 5;
const INDEX = 10;
        
$file = new SplFileObject('/tmp/test.txt', 'w+');

// write to files
for ($i = 0; $i < LINES; $i++) {
    $file->fwrite("line {$i}" . PHP_EOL);
}

// behaviour of next()
$file->rewind();
for ($i = 0; $i < INDEX; $file->next(), $i++);
echo 'next(): ', $file->key(), PHP_EOL;

// behaviour of seek()
$file->rewind();
$file->seek(INDEX);
echo 'seek(): ', $file->key(), PHP_EOL;

Resulted in this output:

next(): 10
seek(): 5

But I expected this output instead:

next(): 5
seek(): 5

PHP Version

PHP 8.1.6

Operating System

Windows 11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions