1

Possible Duplicate:
How can arguments to variadic functions be passed by reference in PHP?

I need to get the values from the arguments of a method or function. I know i can do this with func_get_args(). But i really need to get the arguments by reference. Is there any way to do that?

I currently have this:

<?php
class Test
{
    function someFunc ( $arg1, $arg2 )
    {
        print_r ( func_get_args() );
    }
}

$t = new Test();
$t->someFunc('a1', 'a2');
?>

In my real code the argument values are passed to another class->method(). In there i want to be able to change the argument values. This would be possible if i could get the values as reference.

Is there a solution for this?

Community
  • 1
  • 1
Vivendi
  • 20,047
  • 25
  • 121
  • 196

2 Answers2

1

Nothing stopping you now .. just make it a variable instead if not PHP would return Fatal error: Cannot pass parameter 1 by reference

class Test {

    function someFunc(&$arg1, &$arg2) {
        var_dump(func_get_args());

        $arg1 = strtoupper($arg1);
        $arg2 = strtoupper($arg2);
    }
}

echo "<pre>";

$arg1 = "a1";
$arg2 = "ar2";
$t = new Test();
$t->someFunc($arg1, $arg2);

var_dump($arg1, $arg2);

Output

array (size=2)
  0 => string 'a1' (length=2)
  1 => string 'ar2' (length=3)

string 'A1' (length=2) // modified
string 'AR2' (length=3) // modified
Baba
  • 94,024
  • 28
  • 166
  • 217
-1
    <?php
    class Test
    {
        function someFunc ( &$arg1, &$arg2)
        {
//    used the variable as per you want it....
        }
    }

    $t = new Test();
    $t->someFunc('a1', 'a2');
    ?>


    You have to add & to the arguement so that it can be used as reference...
THE ONLY ONE
  • 2,110
  • 1
  • 12
  • 6