PHP on the Backend (part 2)
To run the class I posted yesterday, I typically use a class called DaemonRunner. This class sets up for proper signal handling, and 'executes' the class extended from Daemon.
declare(ticks=1); class DaemonRunner { public static function exec($className) { $argv = $_SERVER['argv']; $daemon = new $className($argv); $daemon->init(); while (!$daemon->isDone()) { $daemon->run(); } $daemon->shutdown(); } }
The first statement is a PHPism. This allows PHP to check for signals every 1 tick. A tick is simply a low-level PHP interpreter step. The way I've setup my daemon and signal handling by default, the PHP will finish its current iteration and then quit when receiving a signal. This ensures that the database, and whatever background stuff aren't left in inconsistent states. As with anything, you're free to modify, extend, or otherwise alter this behavior.