php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49032 SplFileObject::fscanf pass-by-reference values
Submitted: 2009-07-23 11:46 UTC Modified: 2009-07-23 14:43 UTC
From: salathe@php.net Assigned:
Status: Closed Package: SPL related
PHP Version: 5.*, 6CVS (2009-07-22) OS: *
Private report: No CVE-ID: None
 [2009-07-23 11:46 UTC] salathe@php.net
Description:
------------
SplFileObject::fscanf should behave identically to fscanf but that does not appear to be the case. When attempting to provide variables into which values should be assigned, the script either warns of undefined variables (when passing apparently by value?) or throws out call-time pass-by-reference E_DEPRECATED errors (when forcing pass-by-reference).

Reproduce code:
---------------
// fruits.txt
apple,100
banana,21

Pass by value:
<?php
$file = new SplFileObject("fruits.txt");
while (!$file->eof()) {
	var_dump($file->fscanf("%[a-z],%d", $fruit, $quantity), $fruit, $quantity);
}
?>

Pass by reference:
<?php
$file = new SplFileObject("fruits.txt");
while (!$file->eof()) {
	var_dump($file->fscanf("%[a-z],%d", &$fruit, &$quantity), $fruit, $quantity);
}
?>

Expected result:
----------------
Pass by value (the method definition should take variables by reference):

int(2)
string(5) "apple"
int(100)
int(2)
string(6) "banana"
int(21)

Pass by reference:

Should report E_DEPRECATED call-time pass-by reference. Which it does.

Actual result:
--------------
Pass by value:

<br />
<b>Notice</b>:  Undefined variable: fruit in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined variable: quantity in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined variable: fruit in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined variable: quantity in <b>fruits.php</b> on line <b>4</b><br />
int(2)
NULL
NULL
<br />
<b>Notice</b>:  Undefined variable: fruit in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined variable: quantity in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined variable: fruit in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Notice</b>:  Undefined variable: quantity in <b>fruits.php</b> on line <b>4</b><br />
int(2)
NULL
NULL




Pass by reference:
<br />
<b>Deprecated</b>:  Call-time pass-by-reference has been deprecated in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Deprecated</b>:  Call-time pass-by-reference has been deprecated in <b>fruits.php</b> on line <b>4</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at fruits.php:4) in <b>fruits.php</b> on line <b>1</b><br />
int(2)
string(5) "apple"
int(100)
int(2)
string(6) "banana"
int(21)



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-23 14:42 UTC] svn@php.net
Automatic comment from SVN on behalf of jani
Revision: http://svn.php.net/viewvc/?view=revision&revision=284648
Log: - Fixed bug #49032 (SplFileObject::fscanf() variables passed by reference)
 [2009-07-23 14:43 UTC] jani@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC