Wrapper Thread class for function with variable arguments in PHP
The idea here is to create a class that builds with a function and an array of parameters and calls that run on a new thread.
This is my class:
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction($this->aParmeters[0], $this->aParmeters[1], ...);
}
}
Obviously the launch function is wrong, which leads me to my question:
Assuming the array is guaranteed to have the correct number of elements according to the function being called, how can I call a function in PHP with an unknown number of arguments that are stored in the array?
Edit: Also I don't have access to the content of the given function, so it cannot be edited.
Edit 2: I'm looking for something similar to the curry function of the schema.
source to share
As of PHP 5.6, this is now possible. Arrays can be expanded into an argument list using the ... operator like so:
<?
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction(... $this->aParmeters);
}
}
?>
See here for more information
source to share
I think that the function should take in its case an array arg
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction($this->aParmeters);
}
public function fFunction($arr){
$var0 = $arr[0];
$var1 = $arr[1];
...
do
..
}
}
source to share