gpt4 book ai didi

php - 一个参数数目未知的方法可以调用另一个参数数目未知的方法吗?

转载 作者:行者123 更新时间:2023-12-04 05:39:06 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




9年前关闭。




Possible Duplicate:
PHP, how to pass func-get-args values to another function as list of arguments?



我在基类中有一个方法可以接受任意数量的参数。此方法需要调用第三方对象的方法,该方法可以使用传递给第一个方法的参数接受任意数量的参数。

我提到它是一个第三方对象,将被调用以加强被调用方法的签名不能修改为接受数组或对象的约束。

示例:
<?php
class Example {

private $thirdPartyObject = null;

public function methodOne() {
$arguments = func_get_args();

$this->thirdPartyObject = new ThirdPartyObject();
$this->externalObject->methodName(/* pass on variable number of arguments here */);
}
}

$exampleObject = new Example();
$exampleObject->methodOne('a', 'b', 'c');

如果我们事先知道传递给 Example->methodOne() 的参数数量然后我们可以将相同数量的参数传递给 ThirdPartyObject->methodName() .

如果我们事先不知道传递给 Example->methodOne() 的参数数量,我们可以将这些参数传递给 ThirdPartyObject->methodName() ?

在这种情况下, ThirdPartyObject->methodName()使用一个或多个参数调用,例如:
<?php
$thirdPartyObject = new ThirdPartyObject();
$thirdPartyObject->methodName('a');
$thirdPartyObject->methodName('a', 'b');
$thirdPartyObject->methodName('a', /* ... */, 'N');

最佳答案

你说的是call_user_func_array (), 我认为。但是使用它并不是一个好习惯。这是相当缓慢的。

call_user_func_array(array($this->externalObject, "methodName"), $arguments);

关于php - 一个参数数目未知的方法可以调用另一个参数数目未知的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11507152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com