(Yes, I know, I've done no blogging in far too long. I've got a stack of stuff to blog about, but it's all rather heavy. In the mean time, here's something light.)

When executing code at the command line using php -r and PHP 5.2.5, be sure not to extend the Exception class. It will cause a segmentation fault.

For example, the following causes no trouble at all:


Samurai:~ pmjones$ php -r "throw new Exception();"
PHP Fatal error:  Uncaught exception 'Exception' in Command line code:1
Stack trace:
#0 {main}
  thrown in Command line code on line 1

But the next example gives a segmentation fault following a long ... pause ... after the stack trace output:


Samurai:~ pmjones$ php -r "class Foo extends Exception {} throw new Exception();"
Fatal error: Uncaught exception 'Exception' in Command line code:1
Stack trace:
#0 {main}
  thrown in Command line code on line 1
Segmentation fault

Note that we didn't even throw the extended Foo exception; we threw the native PHP exception. The mere presence of the extended class is enough to cause the segfault.

It took me two evenings to track this down; what you see here is the simplified generic case. I've entered a bug with the PHP guys here.

Update: I thought I was running 5.2.6, but I was wrong; this was occurring on PHP 5.2.5. Note to self: check to make sure you're running the latest version. :-)

Update (2008-08-12): These guys found the problem earlier, too: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/198246.

Are you stuck with a legacy PHP application? You should buy my book because it gives you a step-by-step guide to improving you codebase, all while keeping it running the whole time.