Skip to main content
Topic: pass by ref (Read 5549 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

pass by ref

 emanuele 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?
Bugs creator.
Features destroyer.
Template killer.

Re: pass by ref

Reply #1

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?
Bugs creator.
Features destroyer.
Template killer.

Re: pass by ref

Reply #2

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.