Re[2]: Passing an indefinite number of parameters by reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Monday, May 8, 2006, 8:46:38 PM, you wrote:
CJ> Tom Rogers wrote:
>> Here is a cutdown version of a class loader I use, It works for php4 and
>> php5 and with references. It should be easy to modify.

CJ> [...]

CJ> Sorry, this does not work with references as func_get_args() copies the
CJ> arguments passed, rather than keeping them as references. This means 
CJ> that the $variable1, $variable2, etc. variables are only references to
CJ> the copy made with func_get_args(), rather than to the variables passed
CJ> to the first function.

CJ> Chris


Here is a modified version using arrays (tested php4/5)

<?php
$a = 1;
$b = 1;
function test_vars($function, $array){
  $num_args = count($array);
  $vars = $function.'(';
  if($num_args > 0) {
    for ($i=0; $i < $num_args; $i++) {
      $vars .= ($i > 0)? ',':'';
      $varname = 'variable'.$i;
      $$varname =& $array[$i];
      $vars .= "\$$varname";
    }
  }
  $vars .= ');';
  eval($vars);
}
function test(&$a){
  $a++;
}
function test2(&$a, &$b){
  $a++;
  $b++;
}
echo "$a\n";
test($a);
echo "$a\n";
test_vars('test',array(&$a));
echo "$a\n";
test_vars('test2',array(&$a,&$b));
echo "$a : $b\n";

// a test on a function we have no control over
$string = "This is a test";
$match = array();
test_vars('preg_match',array('/(\w+) (\w+) (\w+) (\w+)/',$string,&$match));
print_r($match);

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux