ElkArte Community

Elk Development => Bug Reports => Exterminated Bugs => Topic started by: emanuele on January 12, 2013, 01:38:31 pm

Title: pass by ref
Post by: emanuele on January 12, 2013, 01:38:31 pm
/me is now completely confused...

Code: [Select]
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
works.

Code: [Select]
function foo(&$var)
{
    $var++;
}

$a=5;
call_user_func_array('foo', array($a));
QuoteWarning: Parameter 1 to foo() expected to be a reference, value given in blabla.php on line 10

But if I understood correctly (even though it may not be the case at that point) if it is changed to:
Code: [Select]
function foo($var)
{
    $var++;
}

$a=5;
call_user_func_array('foo', array(&$a));
it should raise the:
QuoteNotice: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of call_integration_hook(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file

Did I miss anything?
Title: Re: pass by ref
Post by: emanuele on January 13, 2013, 07:06:10 am
So:
Code: [Select]
function foo($var)
{
    $var++;
}

$a=5;
call_user_func_array('foo', array(&$a));
is fine and
Code: [Select]
function foo($var)
{
    $var++;
}

$a=5;
call_user_func_array('foo', &$a);
is broken?...and what's the difference in practical terms?
What's the most appropriate approach?
Title: Re: pass by ref
Post by: Arantor on January 31, 2013, 02:58:54 pm
The correct approach is that the receiving function should always indicate its requiring a reference, making the whole thing moot.

Also, the difference between using array(&$a) and &$a is quite important - &$a then needs to contain all the parameters you're sending, rather than being able to send multiple things through in a nicely delineated fashion.