27

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.

GetFree
  • 40,278
  • 18
  • 77
  • 104
  • 1
    Your question is kind of unclear... mainly because there are some missing words and punctuation. – Tyler Carter Dec 17 '09 at 23:15
  • If you want an array of references (which points to other variables), then I'm not sure if this can be done in PHP (?) – Steven Dec 17 '09 at 23:20
  • Maybe the OP should come back to rephrase the question so that we are able to actually answer it. :) – user103219 Dec 17 '09 at 23:38
  • 1
    Maybe you could give us an example of why passing an array or an object wrapping the elements doesn't suit you. – Juan Dec 17 '09 at 23:54
  • Btw PHP 5.6 has a new "splat operator" http://stackoverflow.com/a/21387043/632951 – Pacerier Oct 07 '14 at 14:51

5 Answers5

19

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 ;)

Bob Fanger
  • 28,949
  • 7
  • 62
  • 78
8

PHP 5.6 introduces a new variadic syntax that supports pass-by-reference. Prefixing the last parameter to a function with ... declares it as an array that will hold any actual arguments from that point on. The array can be declared to hold references by further prefixing the ... token with a &, as is done for other parameters, effectively making the arguments pass-by-ref.

Example 1:

function foo(&...$args) {
    $args[0] = 'bar';
}
foo($a);
echo $a, "\n";
#  output:
#a

Example 2:

function number(&...$args) {
    foreach ($args as $k => &$v) {
        $v = $k;
    }
}
number($zero, $one, $two);
echo "$zero, $one, $two\n";
#  output:
#0, 1, 2
outis
  • 75,655
  • 22
  • 151
  • 221
1

You should be able to pass all of your parameters wrapped in an object.


Class A
{
    public $var = 1;
}

function f($a)
{
    $a->var = 2;
}

$o = new A;
printf("\$o->var: %s\n", $o->var);
f($o);
printf("\$o->var: %s\n", $o->var);

should print 1 2

BojanG
  • 1,872
  • 1
  • 15
  • 23
0

It is possible:

$test = 'foo';
$test2 = 'bar';

function test(){
    $backtrace = debug_backtrace();
    foreach($backtrace[0]['args'] as &$arg)
        $arg .= 'baz';
}

test(&$test, &$test2);

However, this uses call-time pass by reference which is deprecated.

newacct
  • 119,665
  • 29
  • 163
  • 224
-1

Edit: sorry I didn't see you wanted them to be references....all you have to do is pass them as an object.

You can also pass them in an array for example

myfunction(array('var1' => 'value1', 'var2' => 'value2'));

then in the function you simply

myfunction ($arguments) {

echo $arguments['var1'];
echo $arguments['var2'];

}

The arrays can also be nested.

user103219
  • 3,209
  • 11
  • 39
  • 50
  • I actually am still not 100% sure what your trying to ask, but I hope I understood correctly. – user103219 Dec 17 '09 at 23:30
  • The question is simple. The hard thing is to explain why passing the arguments wrapped in an array or in an object don't serve my needs (I's a thing about argument binding and function composition). – GetFree Dec 17 '09 at 23:41
  • This is not correct! Arrays are passed by value in PHP. – Victor Schröder Jun 06 '17 at 15:16