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
 [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

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC