php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52277 fatal error with call_user_func and function with by-reference arguments
Submitted: 2010-07-07 18:06 UTC Modified: 2010-07-07 18:14 UTC
From: abautu at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: Ubuntu 10.04
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: abautu at gmail dot com
New email:
PHP Version: OS:

 

 [2010-07-07 18:06 UTC] abautu at gmail dot com
Description:
------------
I have a Drupal site. It worked ok until I upgraded php to 5.3.2. It simply fails, nothing is written to the error log or on the screen.

I tracked down the problem to the following point: 
 * in some file, there is this call: 
     return call_user_func($hook_function, $variables, $suggestions);
 where $hook_function is _phptemplate_page
 * _phptemplate_page function is defined as: 
    function _phptemplate_page(&$variables, &$suggestions) {
     ....
    }
If I remove the & before variables, the site works. I know the by-reference variable behavior changed in 5.3.2 from 5.2, but in this case, the actual parameters are still 2 variables (not constants).

Cheers,
Andrei

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

function testfnc1($x) {
  echo "testfnc1: $x ";
}

function testfnc2(&$x) {
  echo "testfnc2: $x ";
}

$y = 'works';
call_user_func('testfnc1', $y);
call_user_func('testfnc2', $y);

Expected result:
----------------
testfnc1: works testfnc2: works 

Actual result:
--------------
testfnc1: works 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-07 18:14 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-07-07 18:14 UTC] rasmus@php.net
Passing a static string into a function that takes the parameter by reference 
makes no sense. If that function were to modify the passed parameter, there would 
be no place to store those changes, so this should fail.  You also get a nice fat 
warning that looks like this:

Warning: Parameter 1 to testfnc2() expected to be a reference, value given

So make sure you have your error_reporting() level set high enough.
 [2010-07-26 10:42 UTC] dagdamor10 at mail dot ru
Actually, the reason is different.

The reason of this is error is that bug reporter was using call_user_func() instead of calling the desired function directly. call_user_func() can't pass vars by reference; it simply cannot do it. So the $y gets passed by value instead, causing an error. If you need to workaround this, you should use call_user_func_array():

call_user_func_array('testfnc2', array(&$y)); // Note the &

This should work alright. :)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Aug 18 13:00:02 2025 UTC