|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63998 At least since PHP 5.3.10 parameters not by reference in call_user_func_array
Submitted: 2013-01-15 21:10 UTC Modified: 2013-01-16 09:54 UTC
From: vtamara at pasosdeJesus dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3.20 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: vtamara at pasosdeJesus dot org
New email:
PHP Version: OS:


 [2013-01-15 21:10 UTC] vtamara at pasosdeJesus dot org
From manual page:

The test script under PHP 5.2.17 produces the expected result shown below.

However at least with PHP 5.3, it produces the actual result shown.

This change in behavior from PHP 5.2 to PHP 5.3 is not documented (it is 
documented for PHP 5.4).

Test script:
function x(&$a) {
        return array('x');

$y = 'y';
echo "1\n"; var_dump(x($y));
echo "2\n";
var_dump(call_user_func_array('x', array($y)));

Expected result:
array(1) {
  string(1) "x"
array(1) {
  string(1) "x"

Actual result:
array(1) {
  string(1) "x"
PHP Warning:  Parameter 1 to x() expected to be a reference, value given in 
/home/vtamara/comp/php/callback.php on line 10


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-16 09:54 UTC]
To be clear this is not a bug. You're simply confusing two different behaviors.

What you're describing in your example is not call-time pass-by-reference behavior. What you point 
out in the documentation does indeed refer to the changes in call-time pass-by-reference, which did 
in fact change in PHP 5.4.0.

Call-time pass-by-reference is when you call the function with your arguments passed to the function 
by reference (and not when your function prototype requires arguments to be passed by reference). 
These are two difference behaviors.

One is "pass-by-reference" and the other is "call-time pass-by-reference". The difference is that in 
the latter the references are created at the time you invoke the function (or at the time you "call" 
on the function). The former creates the references in the function prototype making them always 
available to the caller.

The form of call-time pass-by-reference this note in the manual refers to was deprecated in PHP 5.3, 
but did not issue an E_DEPRECATED error. It was removed in PHP 5.4, but the E_WARNING error you're 
referring to is not about call-time pass-by-reference. It's the fact that your function expects a 
reference and you are passing a value. This is a result of how call_user_func_array has changed from 
allowing arguments to always be passed by reference to the callback, regardless of what the callback 
function expects -- to no longer allowing this behavior by default.

Here's an example of call-time pass-by-reference behavior:

function x($a) { // notice the function does not require a reference
    return ++$a;

$y = 1;

/* Notice we are passing by reference at call-time */

var_dump(x(&$y)); // int(2)
var_dump($y);     // int(2)

/* Notice the &$y -- again this is call-time pass-by-reference */
         call_user_func_array('x', array(&$y)), // int(3)
         $y                                     // int(3)

In PHP >= 5.4.0 the above code would produce a fatal error of:

"Fatal error: Call-time pass-by-reference has been removed; If you would like to pass argument by 
reference, modify the declaration of x() ..." for line 11 where we call var_dump(x(&$y)). As for 
call_user_func_array(), you would not get a fatal error or a warning, but $y will still show int(1) 
since nothing is actually referenced here.

It's the case that in your test script call_user_func_array actually failed to call your function and 
the reason why you see NULL as the return value.
 [2013-01-16 09:54 UTC]
-Status: Open +Status: Not a bug
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Apr 15 12:01:24 2021 UTC