Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取参数值作为PHP中的引用_Php_Reference - Fatal编程技术网

获取参数值作为PHP中的引用

获取参数值作为PHP中的引用,php,reference,Php,Reference,可能重复: 我需要从方法或函数的参数中获取值。我知道我可以用func_get_args做到这一点。但我真的需要通过引用获取参数。有办法吗 我目前有: <?php class Test { function someFunc ( $arg1, $arg2 ) { print_r ( func_get_args() ); } } $t = new Test(); $t->someFunc('a1', 'a2'); ?> 在我的实际代码中

可能重复:

我需要从方法或函数的参数中获取值。我知道我可以用func_get_args做到这一点。但我真的需要通过引用获取参数。有办法吗

我目前有:

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

$t = new Test();
$t->someFunc('a1', 'a2');
?>
在我的实际代码中,参数值被传递给另一个类->方法。在这里,我希望能够更改参数值。如果我能得到这些值作为参考,这是可能的


有解决办法吗?

现在没有什么能阻止你。。如果不是PHP将返回致命错误:无法通过引用传递参数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...
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);
array (size=2)
  0 => string 'a1' (length=2)
  1 => string 'ar2' (length=3)

string 'A1' (length=2) // modified
string 'AR2' (length=3) // modified