|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-11-20 20:48 UTC] tim_siebels_aurich at yahoo dot de
Description:
------------
Iterating over a SplFileObject differs from using fopen() and friends.
SplFileObject is supposed to be an object oriented interface for the file handling functions. Their output shouldn't be different.
Test script:
---------------
<?php
$o = new SplFileObject(__FILE__);
foreach($o as $key=>$val) {
var_dump($val);
}
/*
Should be the same as
$h = fopen(__FILE__, 'r');
while(!feof($h)) {
var_dump(fgets($h));
}
*/
Expected result:
----------------
string(3) "<?
"
string(34) "$o = new SplFileObject(__FILE__);
"
string(1) "
"
string(28) "foreach($o as $key=>$val) {
"
string(18) " var_dump($val);
"
string(2) "}
"
bool(false)
Actual result:
--------------
<?php
$o = new SplFileObject(__FILE__);
foreach($o as $key=>$val) {
var_dump($val);
}
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 16:00:01 2025 UTC |
Hi, This is the equivalent you're looking for: <?php $o = new SplFileObject(__FILE__); while (!$o->eof()) { var_dump($o->fgets()); } ?> However, it *is* weird that the iterator doesn't stop at the end of file.