views:

223

answers:

5

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
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
I believe he means the timer stuff.
Alix Axel
@Sahil: You can use `register_tick_function()` and check for the right timestamp to execute.
Alix Axel
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
Oh God.. You can do that? Wow! I will try it out and come back to you.
5lackp1x3l0x17
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
This runs the functions sequentially, not at the same time.
David Dorward
+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
this is based on the assumption that you want to run the functions at the same time (simultaneouesly).
Anurag
Which is a fair assumption since the question explicitly says that.
David Dorward
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
A: 

i'am from indonesia, thanks so much

Amin-v