|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-10-21 19:34 UTC] lucasfbustamante at gmail dot com
Description: ------------ While fixing https://bugs.php.net/bug.php?id=62004, PHP 8.0.1 probably introduced a bug, where after using `SplFileObject::seek($line)`, the first subsequent call to `SplFileObject::fgets()` does not increase the line pointer. Test script: --------------- $file = new \SplTempFileObject(); for ($i = 0; $i < 100; $i++) { $file->fwrite("Foo\n"); } $file->seek(50); var_dump($file->key()); var_dump($file->fgets()); var_dump($file->key()); var_dump($file->fgets()); var_dump($file->key()); // https://3v4l.org/bX3E0 Expected result: ---------------- I expect that using `SplFileObject::fgets()` will consistently increment the line pointer by one. Actual result: -------------- The first call to `SplFileObject::fgets()` after calling `SplFileObject::seek($line)` will not increment the lint pointer. Subsequent calls will. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 12:00:01 2025 UTC |
I don't believe that is correct. It's a bit tricky because key/current/next/etc. are almost always called through iteration with foreach and never manually, and when calling them manually you're expected to follow a standard pattern: for ($object->rewind(); $object->valid(); $object->next()) { $key = $object->key(); $value = $object->current(); // ... } or in other words, 1. rewind 2. valid 3. key/current 4. next 5. goto 2 However it should not matter if you call key/current before the first next because those are supposed to be read-only operations, yet here it does matter: https://3v4l.org/BVMsV vs https://3v4l.org/s1G2E Note that it is incorrect to call methods like next in the same statement as key/current because PHP does not guarantee that it will evaluate all operands in a statement in a particular order (though it virtually always does go left to right). That means with a line like array('triggerNext' => $file->next(), 'line' => $file->key(), 'contents' => trim($file->current())), you can't actually be sure what methods will be called in what order, and having next before or after key/current makes a difference. The same problem exists with expressions like ($i++) + (++$i). https://en.wikipedia.org/wiki/Sequence_point