Sta*_*arx 5 php arguments function
我将用一个接受任意数量函数的简单函数来解释这个问题
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....
}
Run Code Online (Sandbox Code Playgroud)
我希望现在的问题很清楚,这种情况的解决方法是什么?
我忘了提到我调用的下一个函数是另一个类的构造函数类.就像是
$newclass = new class($newarguments);
Run Code Online (Sandbox Code Playgroud)
Yos*_*shi 12
用于简单的函数调用
使用call_user_func_array,但不要破坏 args,只需将剩余的args数组传递给call_user_func_array
call_user_func_array('anotherFunction', $args);
Run Code Online (Sandbox Code Playgroud)
用于创建对象
use:ReflectionClass :: newInstanceArgs
$refClass = new ReflectionClass('yourClassName');
$obj = $refClass->newInstanceArgs($yourConstructorArgs);
Run Code Online (Sandbox Code Playgroud)
或:ReflectionClass :: newinstance
$refClass = new ReflectionClass('yourClassName');
$obj = call_user_func_array(array($refClass, 'newInstance'), $yourConstructorArgs);
Run Code Online (Sandbox Code Playgroud)