PHP: Process Control Functions

I had quite forgotten that PHP can fork a process just like in C! This might be a useful thing to know for doing background tasks, but to be honest the extra complexity required will probably put me off doing it. That’s where this Thread class comes in handy..

include ("threadClass.php");

class testThread extends Thread {

  function testThread($name) {
    $this->Thread($name); // calls the parent constructor and assign its name

  function run() {
    while(true) {
      print time() ."-" . $this->getName() . " said ok...\n"; // every second we're going to print this line...
// Main program. Bring up two instances of the same class (testThread).
// They runs concurrently. It's a multi-thread app with a few lines of code!!!

$test1 = new testThread ("Thread-1");
$test2 = new testThread ("Thread-2");

print "This is the main process. Press [CTRL-CANC] to terminate.\n";
while(true) {sleep(1);}


Author: Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Google+ and Twitter.

1 thought on “PHP: Process Control Functions”

  1. I posted about the same topic a couple of days ago. It’s a great class. As soon as I get over some of the routing projects I got goin’ on, I plan on messing with it a little.


Leave a Reply