PHP中带有可变参数的函数的Thread Wrapper类

时间:2014-08-07 15:50:49

标签: php arrays multithreading pthreads variadic-functions

这里的想法是创建一个用函数构造的类,以及在新线程中运行的参数和调用数组。

到目前为止,这是我的班级:

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], ...);
    }
}

显然,运行功能不正确,这让我想到了问题:

假设阵列保证具有与正在调用的函数匹配的适当数量的元素,如何在PHP中使用存储在数组中的未知数量的参数调用函数?

编辑: 此外,我无法访问给定功能的内容,因此无法进行编辑。

编辑2:我正在寻找与计划咖喱功能类似的东西。

2 个答案:

答案 0 :(得分:1)

从PHP 5.6开始,现在可以实现。可以使用...运算符将数组扩展到参数列表中,如下所示:

<?
class FunctionThread extends Thread {

    public function __construct($fFunction, $aParameters){

        $this->fFunction = $fFunction;
        $this->aParameters = $aParameters;
    }

    public function run(){

        $this->fFunction(... $this->aParmeters);
    }
}
?>

有关详细信息,请参阅here

答案 1 :(得分:0)

我认为该函数应该接受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
        ..
    }

}