Edit report at http://bugs.php.net/bug.php?id=52277&edit=1

 ID:                 52277
 Comment by:         dagdamor10 at mail dot ru
 Reported by:        abautu at gmail dot com
 Summary:            fatal error with call_user_func and function with
                     by-reference arguments
 Status:             Bogus
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Ubuntu 10.04
 PHP Version:        5.3.2
 Block user comment: N

 New Comment:

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. :)


Previous Comments:
------------------------------------------------------------------------
[2010-07-07 18:14:48] ras...@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-07 18:06:15] 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 


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52277&edit=1

Reply via email to