Function with an indefinite number of reference arguments

Actually, the subject wrote the function of recursive stripslashes here, I thought that it would be nice to write it in one call. But: if there is only one argument, everything is fine

function a(&$link) {
  $link = 'a';
  }

If more, problems begin:

function upcase() {
  $args = &func_get_args();
  foreach ($args as &$arg)
    $arg = strtoupper($arg);
  }
$a = $b = $c = 'lol';
upcase(&$a, $b, $c); // &$a  ,    allow_call_pass_time_reference .       )
var_dump($a, $b, $c); // 'lol', 'lol', 'lol'...

I found this solution so far:

function upcase(&$args) {
  if (is_array($args)) {
    foreach ($args as &$arg)
      upcase($arg);
    } else $args = strtoupper($args);
  }
upcase($a = array(&$_GET, &$_POST, &$_REQUEST));
var_dump($_GET);

But in my opinion the solution is dirty and vulgar.

Any suggestions?


Answer 1, authority 100%

A couple of hours of googling gave this result. It’s not perfect, but it works)

<?
/*
 :  , 
    .
 ,     
  allow_call_pass_time_reference
,  -   
,  #2   )
*/
function upcase() {
  $stack = debug_backtrace();
  foreach ($stack[0]["args"] as &$arg)
    $arg = strtoupper($arg);
  }
$a = $b = $c = 'lol';
upcase(&$a, &$b, &$c);
var_dump($a, $b, $c);
echo '<br />';
/*
 2 -   ,
        
  (  )
+  
*/
function upcase2() {
  $result = array();
  foreach (func_get_args() as $arg)
    $result[] = strtoupper($arg);
  return $result;
  }
$a = $b = $c = 'lol';
list($a, $b, $c) = upcase2($a, $b, $c);
var_dump($a, $b, $c);
/*
 :
string(3) "LOL" string(3) "LOL" string(3) "LOL"
string(3) "LOL" string(3) "LOL" string(3) "LOL"
*/
?>

Answer 2

Oooooh!!!!! Finally a worthwhile question!

The fact is that in php you cannot return a reference to simple types.

Let’s write a couple of lines of code for the test:

function proxy(&$a)
{
    $b = &$a;
    return $b;
}
$a1 = 'aaa';
echo $a1."\n";
$a2 = proxy($a1);
$a2 = 'zzz';
printf("[%s][%s]\n", $a1, $a2);
echo "Test class\n";
class TestProxy { public $t = ''; public function __construct($t){ $this->t = $t; } }
$b1 = new TestProxy('aaa');
echo $b1->t."\n";
$b2 = proxy($b1);
$b2->t = 'zzz';
printf("[%s][%s]\n", $b1->t, $b2->t);

Now let’s analyze the output:

aaa
[aaa][zzz]
Test class
aaa
[zzz][zzz]