Edit report at https://bugs.php.net/bug.php?id=65492&edit=1
ID: 65492 Comment by: bixuehujin at gmail dot com Reported by: foo at example dot com Summary: Seeking beyond EOF leads to inconsistent results for key() and current() Status: Open Type: Bug Package: SPL related Operating System: any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Sorry, the expect result should be: Expect result: ---------------- string(4) "foo " string(4) "bar " Previous Comments: ------------------------------------------------------------------------ [2013-09-19 18:55:40] bixuehujin at gmail dot com Another inconsistent: Script: ---------------- $txt = <<< TXT foo bar baz TXT; $file = new SplTempFileObject(-1); $file->fwrite($txt); $file->seek(0); var_dump($file->fgets()); $file->seek(1); var_dump($file->fgets()); Expect result: ---------------- string(4) "foo " string(3) "bar " Actual result: -------------- string(4) "foo " string(3) "baz" BTW: --------------- The issue also existed in SplFileObject. ------------------------------------------------------------------------ [2013-08-21 06:56:34] foo at example dot com Sorry, the last line in the Test Script should read var_dump( $file->key(), $file->current() ); The problem does exist for fgetcsv() though, too. It will return NULL instead of the data at the last line. ------------------------------------------------------------------------ [2013-08-21 06:50:48] foo at example dot com Description: ------------ When seek()'ing beyond the end of file in an SplFileObject, SplFileObject::key() will return the number of the last line in the file but SplFileObject::current() will return false. This is inconsistent. Either SplFileObject::current() returns the content of the last line then, too. Or SplFileObject::key() should return false as well. As an alternative, SplFileObject::seek() could raise an OutOfBoundsException when trying to seek beyond EOF. Test script: --------------- $txt = <<< TXT foo bar baz TXT; $file = new SplTempFileObject(-1); $file->fwrite($txt); $file->seek(100); var_dump( $file->key(), $file->fgetcsv()); Expected result: ---------------- int(2) baz or false false or OutOfBoundsException Actual result: -------------- int(2) NULL ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=65492&edit=1