如何在php中通过引用传递无限参数

发布于 2024-11-28 23:43:33 字数 293 浏览 0 评论 0原文

我有一个 php 函数,它有无限数量的参数,我从 func_get_args() 获得这些参数。我有一些带有参数的操作(更改字符串或执行某些操作),我希望这就像通过引用传递参数一样。是否可以?

例子:

$test = 'foo';
$test2 = 'bar';

function test(){
    $args = func_get_args();
    foreach($args as $arg)
        $arg .= 'baz';
}

test($test, $test2);

I have php function that has an unlimited number of args which I am getting from func_get_args(). I have some operations with arguments (changing string or doing something) and I want this to be like a passing argument by reference. is it possible?

example:

$test = 'foo';
$test2 = 'bar';

function test(){
    $args = func_get_args();
    foreach($args as $arg)
        $arg .= 'baz';
}

test($test, $test2);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

时间你老了 2024-12-05 23:43:33

从 PHP-5.6 开始,您可以使用 可变参数 引用:

function test(&...$args) {
    foreach ($args as &$arg) {
        $arg .= 'baz';
    }
}

Since PHP-5.6 you can use a variadic reference:

function test(&...$args) {
    foreach ($args as &$arg) {
        $arg .= 'baz';
    }
}
路还长,别太狂 2024-12-05 23:43:33

正如PHP:按引用可变长度参数列表?中的回答, PHP 中无法组合可变长度并通过引用传递函数参数。相反,链接的答案使用了声明 100 个 &argx 的技巧,然后使用 get_num_args() 来计算实际使用了多少个。恭喜,您发现了 PHP 中的一个特别困难的角落;)

它还展示了如何使用 PHP 5.6+ 变量来做到这一点。

As answered in PHP: variable-length argument list by reference?, there is no way in PHP to combine variable-length and pass by reference function arguments. Instead, the linked answer uses a hack of declaring 100 &argxs, then using get_num_args() to figure out how many were actually used. Congratulations, you found a particularly hard corner in PHP ;)

It also shows how to do it with PHP 5.6+ variadics.

无人接听 2024-12-05 23:43:33

我非常怀疑这是可能的,但我确实知道一种方法可以得到你想要的东西:

function test(&$args) {
    foreach ($args as $arg) {
        $arg .= 'baz';
    }
}

test(array(&$test, &$test2));

I highly doubt that's possible, but I do know one way you could get what you want:

function test(&$args) {
    foreach ($args as $arg) {
        $arg .= 'baz';
    }
}

test(array(&$test, &$test2));
躲猫猫 2024-12-05 23:43:33

这是可行的:

$test = 'foo';
$test2 = 'bar';

function test(){
    $backtrace = debug_backtrace();
    foreach($backtrace[0]['args'] as &$arg)
        $arg .= 'baz';
}

test(&$test, &$test2);

但是,这使用了已弃用的调用时传递引用。

This works:

$test = 'foo';
$test2 = 'bar';

function test(){
    $backtrace = debug_backtrace();
    foreach($backtrace[0]['args'] as &$arg)
        $arg .= 'baz';
}

test(&$test, &$test2);

However, this uses call-time pass by reference which is deprecated.

夏天碎花小短裙 2024-12-05 23:43:33

当我做类似下面的例子的事情时,效果很好。我认为关键是在foreach中设置引用。

$var1 = '%DIR%/test';

replaceParameters(
    $var1, 
    $var2, 
    $var3
);

function replaceParameters(&$variables) {

    $variables = array_filter(func_get_args());

    $parameters = [
        '%DIR%' => __DIR__, 
        '%FILE%' => __FILE__,
    ];

    foreach($variables as &$variable) {
        $variable = str_replace(array_keys($parameters), array_values($parameters), $variable);
    }

}

Works fine for me when doing something like the example below. I think the key is setting the reference in the foreach.

$var1 = '%DIR%/test';

replaceParameters(
    $var1, 
    $var2, 
    $var3
);

function replaceParameters(&$variables) {

    $variables = array_filter(func_get_args());

    $parameters = [
        '%DIR%' => __DIR__, 
        '%FILE%' => __FILE__,
    ];

    foreach($variables as &$variable) {
        $variable = str_replace(array_keys($parameters), array_values($parameters), $variable);
    }

}
朮生 2024-12-05 23:43:33

我也需要这个功能,我想出了一个解决方案。

function sayhello($params) {
    //params hold an unlimited amount of references
}

sayhello([&$one, &$two]);

但这取决于 php >= 5.4。如果您使用的是 <= 5.4,请使用 array() 语法而不是 []。

我喜欢[] 不过,更好了:)

I needed this functionality as well, I came up with a solution.

function sayhello($params) {
    //params hold an unlimited amount of references
}

sayhello([&$one, &$two]);

This depends on php >= 5.4 though. If you're on <= 5.4, use array() syntax instead of [].

I love [] tho, much better :)

留一抹残留的笑 2024-12-05 23:43:33

使用对象是解决方案

class O {
    public $v;
    public function __construct(&$p){
        $this->v = &$p;
    }
}

function append_baz_to_all(){
    foreach(func_get_args() as &$arg){
        $arg->v .= 'baz';
    }
}

function test1(){
    echo "test1\n";
    $a='A';$b='B';$c='C';$d='D';$e='E';$f='F';$g='G';
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n");
    append_baz_to_all(new O($a), new O($b));
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n\n");
}

//shortcutting
function o(&$v){return new O($v);}

function test2(){
    echo "test2\n";
    $a='A';$b='B';$c='C';$d='D';$e='E';$f='F';$g='G';
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n");
    append_baz_to_all(o($c), o($d), o($e));
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n\n");
}


test1();
test2();

Using objects is the solution

class O {
    public $v;
    public function __construct(&$p){
        $this->v = &$p;
    }
}

function append_baz_to_all(){
    foreach(func_get_args() as &$arg){
        $arg->v .= 'baz';
    }
}

function test1(){
    echo "test1\n";
    $a='A';$b='B';$c='C';$d='D';$e='E';$f='F';$g='G';
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n");
    append_baz_to_all(new O($a), new O($b));
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n\n");
}

//shortcutting
function o(&$v){return new O($v);}

function test2(){
    echo "test2\n";
    $a='A';$b='B';$c='C';$d='D';$e='E';$f='F';$g='G';
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n");
    append_baz_to_all(o($c), o($d), o($e));
    echo("\$a=$a \$b=$b \$c=$c \$d=$d \$e=$e \$f=$f \$g=$g\n\n");
}


test1();
test2();
咋地 2024-12-05 23:43:33
function byref_alias()
{
 //$argz=func_get_args(); //byval, not what you want
 $argz=debug_backtrace()[0]['args']; //byref hack
 $argz[0]++; works
}
function byref_alias()
{
 //$argz=func_get_args(); //byval, not what you want
 $argz=debug_backtrace()[0]['args']; //byref hack
 $argz[0]++; works
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文