可能重複:
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');
您是否嘗試過這個?:[http://stackoverflow.com/questions/2126778/php-how-to-pass-func-get-args - 值對另一個功能-AS--的-參數列表(http://stackoverflow.com/questions/2126778/php-how-to-pass-func-get-args-values-to-another- function-as-list-of-arguments) – Zbigniew 2012-07-16 15:11:38
@des:謝謝你的建議,我的搜索沒有找到它。 – 2012-07-16 15:23:56