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?
A:
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.
Sarfraz
2009-12-15 10:26:31
I cant do that, bcoz I dont know initially what functions will run. That will depend on the user. When I did created the same program in C# I used the following code:myTimer.Elapsed += new ElapsedEventHandler(function_name);by this I could add any function sort of dynamically...
5lackp1x3l0x17
2009-12-15 10:30:40
I believe he means the timer stuff.
Alix Axel
2009-12-15 10:42:48
@Sahil: You can use `register_tick_function()` and check for the right timestamp to execute.
Alix Axel
2009-12-15 10:44:05
A:
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?
tim
2009-12-15 10:37:42
Oh God.. You can do that? Wow! I will try it out and come back to you.
5lackp1x3l0x17
2009-12-15 10:39:11
I have suddenly realised that this wont be possible since in my code a function has to sleep, letting the other function to execute..
5lackp1x3l0x17
2009-12-15 10:45:13
+3
A:
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:
Anurag
2009-12-15 10:39:08
this is based on the assumption that you want to run the functions at the same time (simultaneouesly).
Anurag
2009-12-15 10:43:15
Which is a fair assumption since the question explicitly says that.
David Dorward
2009-12-15 10:44:49
A:
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();
Raynet
2009-12-15 10:48:34