Asked  7 Months ago    Answers:  5   Viewed   39 times

Is it possible to create a PHP function that takes a variable number of parameters all of them by reference?

It doesn't help me a function that receives by reference an array of values nor a function that takes its arguments wrapped in an object because I'm working on function composition and argument binding. Don't think about call-time pass-by-reference either. That thing shouldn't even exist.



PHP 5.6 introduced new variadic syntax which supports pass-by-reference. (thanks @outis for the update)

function foo(&...$args) {
    $args[0] = 'bar';

For PHP 5.5 or lower you can use the following trick:

function foo(&$param0 = null, &$param1 = null, &$param2 = null, &$param3 = null, &$param4 = null, &$param5 = null) {
  $argc = func_num_args();
  for ($i = 0; $i < $argc; $i++) {
    $name = 'param'.$i;
    $params[] = & $$name;
  // do something

The downside is that number of arguments is limited by the number of arguments defined (6 in the example snippet). but with the func_num_args() you could detect if more are needed.

Passing more than 7 parameters to a function is bad practice anyway ;)

Wednesday, March 31, 2021
answered 7 Months ago

Remove & from &$this everywhere, it is not needed. In fact, I think you can remove & everywhere in this code - it is not needed at all.

Long explanation

PHP allows to pass variables in two ways: "by value" and "by reference". First way ("by value"), you can't modify them, other second way ("by reference") you can:

     function not_modified($x) { $x = $x+1; }
     function modified(&$x) { $x = $x+1; }

Note the & sign. If I call modified on a variable, it will be modified, if I call not_modified, after it returns the value of the argument will be the same.

Older version of PHP allowed to simulate behavior of modified with not_modified by doing this: not_modified(&$x). This is "call-time pass by reference". It is deprecated and should never be used.

Additionally, in very ancient PHP versions (read: PHP 4 and before), if you modify objects, you should pass it by reference, thus the use of &$this. This is neither necessary nor recommended anymore, as object are always modified when passed to function, i.e. this works:

   function obj_modified($obj) { $obj->x = $obj->x+1; }

This would modify $obj->x even though it formally is passed "by value", but what is passed is object handle (like in Java, etc.) and not the copy of the object, as it was in PHP 4.

This means, unless you're doing something weird, you almost never need to pass object (and thus $this by reference, be it call-time or otherwise). In particular, your code doesn't need it.

Wednesday, March 31, 2021
answered 7 Months ago

The very last parameter, count, is passed by reference. You can see this in the description at where there's a & in front of the variable.

This means you cannot use a literal 1 there. You'd have to do:

$sql = str_replace('?', "'" . $param . "'", $sql, $count);
echo $count;

You'll now have displayed on the screen how many instances were replaced.

Wednesday, March 31, 2021
answered 7 Months ago

After this loop is executed:

foreach ($x as $n => &$v) { }

$v ends up as a reference to $x[2]. Whatever you assign to $v actually gets assigned $x[2]. So at each iteration of the second loop:

foreach ($y as $n => $v) { }

$v (or should I say $x[2]) becomes:

  • 'bye bye'
  • 'world'
  • 'harsh'
Wednesday, March 31, 2021
answered 7 Months ago

Let's pretend $x is a piece of paper with 5 written on it.

function sum($y) {
  $y = $y + 5; 

Here $y is the value of what you have written. You add 5 to such value in your mind, but the note is left untouched.

function sum(&$y) {
  $y = $y + 5; 

With the reference operator (&$y), you pass the very paper to the function, and it overwrites what's written on it.

For primitive values like numbers, I wouldn't bother and always return the value you want:

function valuePlusFive($x) {
  return $x + 5;

$x = 5;
$x = valuePlusFive($x);
Wednesday, March 31, 2021
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :