Running multiple functions in php
I need to run several functions at the same time. I had successfully implemented in C# by creating an ElapsedEventHandler and executing it when a timer gets elapsed. In this way I could run a number of functions at the same time (delegates). How 开发者_开发知识库can I do the same thing using php?
Update
PHP supports multitasking now. See the pthreads API.
PHP does not have multi-threading. So you'd have to spawn another php process through CLI and run that script.
checkout these questions for more info:
- how-can-one-use-multi-threading-in-php-applications
- does-php-have-threading
Something like this should work:
function foo() {
echo "foo\n";
}
function bar() {
echo "bar\n";
}
class multifunc {
public $functions = array();
function execute() {
foreach ($this->functions as $function) $function();
}
}
$test = new multifunc();
$test->functions[] = 'foo';
$test->functions[] = 'bar';
$test->execute();
just create an array where you put all the functions you wanna run, then loop the array and run the functions.
foreach($functions as $func)
{
$func();
}
is that what you wanna do?
This is how you can imitate that:
function runFuncs()
{
function1(); // run funciton1
function2(); // run funciton2
function3(); // run funciton3
function4(); // run funciton4
function5(); // run funciton5
}
When you run runFuncs(); it runs all functions inside it.
精彩评论