螺纹包装类与PHP变量参数的函数 [英] Thread Wrapper Class for a Function with variable arguments in PHP

查看:122
本文介绍了螺纹包装类与PHP变量参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的想法是使用功能和参数数组构造并调用一个新的线程功能的类

The idea here is to make a class that constructs with a function and an array of parameters and calls that function in a new thread.

这是我班至今:

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

显然run函数不正确,这使我对我的问题:

Obviously the run function is incorrect, which brings me to my question:

假设数组保证有元素的适当数量以匹配被调用的函数,我如何可以调用PHP中的函数的参数数目不详的存储阵列中的?

Assuming the array is guaranteed to have the proper number of elements to match the function that is being called, How can I call a function in PHP with an unknown number of arguments that are stored in an array?

编辑:
我也有给定函数​​的内容无法访问,因此不能进行编辑。

Also I have no access to the contents of the given function so it cannot be edited.

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

Edit 2: I'm looking for something similar to scheme's curry function.

推荐答案

在PHP 5.6,这是成为可能。数组可以使用...运算符,像这样被扩展到参数列表:

As of PHP 5.6, this is now possible. Arrays can be expanded into the 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);
    }
}
?>

请参阅这里更多信息

这篇关于螺纹包装类与PHP变量参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆