php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17309 pass by reference in call_user_func
Submitted: 2002-05-20 11:01 UTC Modified: 2002-10-24 16:06 UTC
Votes:3
Avg. Score:4.3 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: pmoor at netpeople dot ch Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.1 OS: linux
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pmoor at netpeople dot ch
New email:
PHP Version: OS:

 

 [2002-05-20 11:01 UTC] pmoor at netpeople dot ch
i've disabled the "call-time pass-by-reference" option in my configuration file.
now, when trying to call_user_func a function with reference-parameters, it won't work, unless i call-time-pass-by-reference my parameters despite the warning.

( this bug might be related to bug #17246 )

here a small example:

function pass_by_reference( &$param ) {
	$param[] = "another entry";
}

$data = array( "a first entry" );
call_user_func( "pass_by_reference", $data );
var_dump($data);	// $data contains only one element ( "a first entry" ) ( == unexpected behaviour )

$data = array( "a first entry" );
call_user_func( "pass_by_reference", &$data ); // Warning: Call-time pass-by-reference has been deprecated - argument passed by value; ...
var_dump($data);	// $data contains both elements ( "a first entry", "another entry" ) ( == expected behaviour )


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-05 11:32 UTC] bram at totalgsm dot net
A workaround might be using variable function name like

$func_name = 'pass_by_reference';
$funcname($data);

However this doesn't work if the $func_name is an array to call a user method (which is a flaw in PHP :( )
 [2002-07-05 11:34 UTC] bram at totalgsm dot net
This might be a duplicate of: 
http://bugs.php.net/bug.php?id=17246

However, this test case is clearer.
 [2002-07-05 11:34 UTC] bram at totalgsm dot net
This might be a duplicate of: 
http://bugs.php.net/bug.php?id=17246

However, this test case is clearer.
 [2002-10-24 16:06 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

When you do ' call_user_func("pass_by_reference", $data ); ' you actually pass the $data variable to the call_user_func first and that function will internally pass $data to the function specified in the first argument. Unless $data is passed to call_user_func by reference, the call_user_func simply makes a copy of the $data variable and passes that copy to the pass_by_reference function. Hence the 'invalid' output, which you see.
If you do ' pass_by_reference( $data ); ' it works, because the function has been defined to accept the parameter by reference.
 [2003-08-30 16:28 UTC] spagmoid at yahoo dot com
This is a good example of why the warning against call-time pass by reference is a bad idea.  Any function that accepts a reference should be required to also have the parms use & at call time.  And references should be allowed as optional parameters ie:

function Blah(&$MayBeBlank=0)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Nov 25 15:01:32 2024 UTC