Skip to content

Different results for seek() on SplFileObject and SplTempFileObject #8563

@gabrielpl

Description

@gabrielpl

Description

The following code:

<?php

declare(strict_types=1);

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

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

// reset
$file_01->rewind();
$file_02->rewind();

// seek
$file_01->seek(INDEX);
$file_02->seek(INDEX);

// show results
echo 'file_01: ' . $file_01->key(), PHP_EOL;
echo 'file_02: ' . $file_02->key(), PHP_EOL;

Resulted in this output:

file_01: 4
file_02: 5

But I expected this output instead:

file_01: 5
file_02: 5

PHP Version

PHP 8.1.6

Operating System

Windows 11

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions