Re[2]: [PHP] Passing an indefinite number of parameters by reference
| From: | Tom Rogers | Date: | Mon, 08 May 2006 17:38:01 +0000 |
| Subject: | Re[2]: [PHP] Passing an indefinite number of parameters by reference | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-235734@lists.php.net to get a copy of this message | ||
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