php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72542 SplFileObject::getCurrentLine() is not an alias of ::fgets()
Submitted: 2016-07-04 13:12 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: cmb@php.net Assigned:
Status: Open Package: SPL related
PHP Version: 7.0.8 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cmb@php.net
New email:
PHP Version: OS:

 

 [2016-07-04 13:12 UTC] cmb@php.net
Description:
------------
The documentation of SplFileObject::getCurrentLine()[1] states
that this method is an alias of SplFileObject::fgets(). However,
that doesn't seem to be the case.

If the method was a true alias, it shouldn't matter whether we
override ::getCurrentLine() or ::fgets() by simply calling the
parent method, but the reported key()s differ (actually,
MyBadFileObject skips every other line number).

I've reported the bug against the latest current stable PHP, but
the behavior is the same for all versions I've tested.

The supplied test script is longer than the postulated 20 lines,
but in my opinion it is easier to read this way.

[1] <http://php.net/manual/en/splfileobject.getcurrentline.php>

Test script:
---------------
<?php

class MyGoodFileObject extends SplFileObject {
    public function fgets() {
        return parent::fgets();
    }
}

class MyBadFileObject extends SplFileObject {
    public function getCurrentLine() {
        return parent::getCurrentLine();
    }
}

function iterateWith($class) {
    $file = new $class(__FILE__);
    $i = 0;
    foreach ($file as $line) {
        echo $file->key(), ' ', $file->current();
        if ($i++ >= 3) break;
    }
    echo PHP_EOL;
}

iterateWith('MyGoodFileObject');
echo '---------------', PHP_EOL;
iterateWith('MyBadFileObject');


Expected result:
----------------
0 <?php
1
2 class MyGoodFileObject extends SplFileObject {
3     public function fgets() {

---------------
0 <?php
1
2 class MyGoodFileObject extends SplFileObject {
3     public function fgets() {

Actual result:
--------------
0 <?php
1
2 class MyGoodFileObject extends SplFileObject {
3     public function fgets() {

---------------
1 <?php
3
5 class MyGoodFileObject extends SplFileObject {
7     public function fgets() {

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 11:01:31 2024 UTC