Search code examples
phpunixdaemons

PHP Daemon in PHP 5.3


While the idea of a PHP daemon has been covered to death on here, I haven't found anything specifically related to how to do this in PHP 5.3. As I've been told, 5.3 introduced new garbage collection/memory management to allow PHP to more cleanly run as a daemon.

I know PHP's no one's first choice for this kind of thing, but in my circumstances it might have to do.

I know in PHP 4, you would have to use something like the System_Daemon class, but I was wondering if that was still needed with the new version of PHP, and wether I'd need to do anything special to use the new features.


Solution

  • PHP uses reference counting for managing allocated memory. When a cycle exists between objects, their reference count is never decremented and the objects are never freed (until the end of the script).

    The only goal of the garbage collector added in PHP5.3 is to kill those cycles. This effectively helps in reducing the memory usage of long running scripts, like daemons.

    Other than that, PHP5.3 adds nothing new for long running scripts / daemons.

    There has been some efforts in making app servers in PHP lately, you may want to look at them:

    https://github.com/indeyets/appserver-in-php