php – 将未定义的参数数转发给另一个函数

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 将未定义的参数数转发给另一个函数脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我将用一个接受任意数量函数简单函数来解释这个问题
function abc() {
   $args = func_get_args();
   //Now lets use the First parameter in something...... In this case a simple echo
   echo $args[0];
   //Lets remove this first parameter 
   unset($args[0]); 

   //Now I want to send the remaining arguments to different function,in the same way as IT received
   .. . ...... BUT NO IDEA HOW TO . ..................

   //tried doing something like this,for a work around
   $newargs = implode(",",$args); 
   //Call Another Function
   anotherFUnction($newargs); //This function is however a constructor function of a class
   // ^ This is regarded as one arguments,not mutliple arguments....

}

我希望现在的问题很清楚,这种情况的解决方法是什么

更新

我忘了提到我调用的下一个函数是另一个类的构造函数类.
就像是

$newclass = new class($newarguments);
用于简单的函数调用

使用call_user_func_array,但不要破坏args,只需将剩余的args数组传递给call_user_func_array

call_user_func_array('anotherFunction',$args);

用于创建对象

用途ReflectionClass::newInstanceArgs

$refclass = new ReflectionClass('yourclassname');
$obj = $refClass->newInstanceArgs($yourConstructorArgs);

或者ReflectionClass::newinstance

$refClass = new ReflectionClass('yourClassName');
$obj = call_user_func_array(array($refClass,'newInstance'),$yourConstructorArgs);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 将未定义的参数数转发给另一个函数全部内容,希望文章能够帮你解决php – 将未定义的参数数转发给另一个函数所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。