Re: Passing an indefinite number of parameters by reference

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

 



On 5/7/06, Chris Jenkinson <chris@xxxxxxxxxxxxx> wrote:
Hi,

Currently I have a function which accepts a limited number of parameters:

call_function($function_name, &$var_1, &$var_2);

I wish to modify the function to accept an indefinite number of
parameters, which may or may not be references.

The function call_function() then calls the function specified in
$function_name, with the variables passed as parameters to that function.

Is this possible? Thanks for any help which can be offered.

Chris

--
Chris Jenkinson
chris@xxxxxxxxxxxxx

"Mistrust all in whom the impulse to punish is powerful."
  -- Friedrich Nietzsche, Thus Spoke Zarathustra

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


One thing you could do is (assuming it's php4)

<?php
function printArray($array) {
   echo '<pre>';
   print_r($array);
   echo '</pre>';
}

function call_function($name, &$params) {
   call_user_func($name, $params);
}



function test(&$params) {
   extract($params, EXTR_REFS);
   printArray($myarray1);
   printArray($myarray2);
   // prove this is a reference by modifying it
   $myarray2 = array('one', 'two', 'three');
}

$myarray1 = array(3, 6, 9);
$myarray2 = array('two', 'four', 'six');
$myParams['myarray1'] =& $myarray1;
$myParams['myarray2'] =& $myarray2;

call_function('test', $myParams);

// myarray2 now has the values set in test
printArray($myarray2);

?>


Output:

Array
(
   [0] => 3
   [1] => 6
   [2] => 9
)

Array
(
   [0] => two
   [1] => four
   [2] => six
)

Array
(
   [0] => one
   [1] => two
   [2] => three
)


This is a bit of a pain though. :)

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