Threads

calendar.php


<?php
  
require('Timer.class.php');
      
  class 
RemindTask extends TimerTask {
    
public
      $message
'';
    
    
public function __construct($message) {
      
$this->message$message;
    }
    
    
public function run() {
      
printf("Reminder: It's now %s, %s\n"date('H:i:s'), $this->message);
      
      
// Return FALSE to indicate no more calls are to be made
      
return FALSE;
    }
  }
  
  class 
Calendar {
    
private
      $timer    
NULL;
      
    
public function __construct() {
      
$this->timer= new Timer();
    }
    
    
public function addTask($task$delay) {
      
$this->timer->schedule($task$delay 1000);
    }
  }

  
// {{{ main
  
print("About to schedule tasks.\n");
  
$r= new Calendar();
  
$r->addTask(new RemindTask('Buy food'), 2);   // in 2 seconds
  
$r->addTask(new RemindTask('Cook it'), 5);    // in 5 seconds
  
$r->addTask(new RemindTask('Meal done'), 8);  // in 8 seconds
  
  
printf("Tasks scheduled at %s.\n"date('H:i:s'));
  
  
// Do something for at least 10 seconds. 
  
foreach (range(110) as $i) {
    print(
date("H:i:s\n"));
    
sleep(1);
  }
  
  print(
"Done.\n");
  
// }}}
?>