Basically this class will allow you to fire off a background process(forking) from another page. I’ve added the ability to optionally ignore the outcome of the process and immediately move onto the next thing or listen for the output of the background process before continuing. Where this type of thing can be helpful is for long, cumbersome processes, that would normally timeout our browser or otherwise force us to wait a long time for it to complete.
An example of this would be if we needed to run a 15 minute SQL query. Instead of waiting 15 minutes for the page to load you can fork the process off to the server and have it send an email when it is finished.
First setup the script that you want to run in the backround. We will call it demo.php. You can use $argv[n] to grab the arguments passed to the script from the main process.
1 2 3 | < ?php echo "Welcome ".$argv[1]." ".$argv[2]."!"; ?> |
Now you can setup the main process or script that will run in the users browser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < ?php require("fork.inc"); /* Initialize the background process by specifying the path to the script it will run */ $test = new Process("demo.php"); /* Define the arguments to run the script with and whether or not to listen for output from the script. */ if($test->fork("John Doe", true)){ print_r($test->output()); } ?> |
This will fork off the demo.php while listening until it returns “Welcome John Doe!”




