获取参数值作为PHP中的引用
可能重复: 我需要从方法或函数的参数中获取值。我知道我可以用func_get_args做到这一点。但我真的需要通过引用获取参数。有办法吗 我目前有:获取参数值作为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'); ?> 在我的实际代码中
<?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