You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SplFileObject: next() moves to inexistent indexes #8564
So, unless ::READ_AHEAD is set, the method just increments the internal line number without checking that the line actually exists. SplFileObject::key() just returns that line number.
This also explains the behavior reported as #8562.
This is fundamentally flawed. ::next() needs to read a line, or at least set a marker that a line has to be read/skipped before ::next() is called again. Apparently, the Iterator interface has been implemented with only Traversable functionality in mind. ::READ_AHEAD has been implemeted somewhat later, probably to somehow cater to this, but I'm having a hard time to claim that multiple consecutive ::next() calls without the ::READ_AHEAD flag being set are simply not supported, or that the behavior reported in this ticket makes any sense.
I think the only way to fix it is to make enable the READ_AHEAD flag by default, […]
Frankly, I think that disabling READ_AHEAD should be deprecated. Unless I'm missing something, there is no point in not reading ahead, since sooner or later it would have to be done anyway.
Not setting READ_AHEAD could be deprecated; of course, that would require lots of code to be adapted, but it might be the only reasonable thing to do in the long run (unless there are plans to deprecate SplFileObject altogether).
READ_AHEAD has other side-effects like producing a phantom empty value on an empty file , so I don't think that's a reasonable replacement at this time.
Activity
gabrielpl commentedon May 16, 2022
Note:
The documentation of next() is incomplete and does not define what should happen in this scenario.
May be:
When eof() is true, next() throws an exception.
In my opinion, option 2 is the most correct.
Either way, the exact behaviour of next() must be explicit stated in the docs
cmb69 commentedon May 16, 2022
php-src/ext/spl/spl_directory.c
Lines 2237 to 2250 in 6beee1a
So, unless
::READ_AHEADis set, the method just increments the internal line number without checking that the line actually exists.SplFileObject::key()just returns that line number.This also explains the behavior reported as #8562.
cmb69 commentedon May 18, 2022
Looking at splfileobject.inc (which is removed as of PHP 7.4.0), confirms the issue:
php-src/ext/spl/internal/splfileobject.inc
Lines 306 to 311 in 7d92153
This is fundamentally flawed.
::next()needs to read a line, or at least set a marker that a line has to be read/skipped before::next()is called again. Apparently, theIteratorinterface has been implemented with onlyTraversablefunctionality in mind.::READ_AHEADhas been implemeted somewhat later, probably to somehow cater to this, but I'm having a hard time to claim that multiple consecutive::next()calls without the::READ_AHEADflag being set are simply not supported, or that the behavior reported in this ticket makes any sense.Girgias commentedon May 27, 2022
I think the only way to fix it is to make enable the READ_AHEAD flag by default, people who want the prior behaviour can always remove it.
But this can only be fixed in 8.2
cmb69 commentedon May 27, 2022
Frankly, I think that disabling
READ_AHEADshould be deprecated. Unless I'm missing something, there is no point in not reading ahead, since sooner or later it would have to be done anyway.Girgias commentedon May 27, 2022
That's also fair, but
READ_AHEADis not the default.cmb69 commentedon May 27, 2022
Yes, I know that. :)
Girgias commentedon Jun 23, 2022
And I don't think we can make
READ_AHEADthe default as it seems to break a hell of a lot of stuffcmb69 commentedon Jun 23, 2022
Not setting
READ_AHEADcould be deprecated; of course, that would require lots of code to be adapted, but it might be the only reasonable thing to do in the long run (unless there are plans to deprecate SplFileObject altogether).pilif commentedon Sep 24, 2025
READ_AHEADhas other side-effects like producing a phantom empty value on an empty file , so I don't think that's a reasonable replacement at this time.Fix phpGH-8563, phpGH-8564: SplFileObject EOF handling for seek() and…
Fix phpGH-8561, phpGH-8562, phpGH-8563, phpGH-8564: SplFileObject ite…
UPGRADING: document SplFileObject iterator fixes in PHP 8.6
UPGRADING: document SplFileObject iterator fixes in PHP 8.6 (#21753)