July, 2006

Treating myself

Sunday, July 16th, 2006

I treated myself today.

For years I’ve wanted to get myself an MP3 player, but I’ve never been able to:

  1. Justify the stupid prices most of them are worth (yes, iPod, I’m looking at you)
  2. Feel comfortable carrying around something worth that much money
  3. Afford the stupid prices most of them are worth.

However, today I found a good deal so now I am the proud owner of a new baby Sandisk Sansa e140, a 1GB flash-based MP3 player. It’s an old model (which is why it was pretty cheap) but based on my first impressions, I love it.

  • It’s seriously small. Not small enough that I’d have to worry about losing it, but small enough that I can put it in a pocket and not be irritated by it. It’s also light as a feather.
  • The included earphones are pretty good - certainly good compared to the cheap crap I usually use, anyway. Nice and bassy, they block out a lot of external noise and they’re comfortable.
  • It came with a case! I hate getting scratches and dirty marks on stuff, so the clear plastic case that came in the box was a godsend.
  • It’s easy to put files on it - just drag-and-drop MP3s to it. It shows up as a normal USB Mass Storage Device (you know, like a USB thumb drive or whatever.)
  • It doesn’t support OGG files, which is mildly irritating, but I hardly have any OGGs anyway.
  • It doesn’t do crossfading either, but I can live with that for the price.
  • The sound boosting settings are nice and work pretty well, but I would’ve liked a ‘dance’ preset on the equaliser.
  • The FM radio is very handy. My phone has an FM radio but I can never be bothered to carry the special attachment you need to use it (you can’t just plug in a normal 3.5mm plug.)
  • It came with the older 1.x firmware, and I haven’t worked up the courage to flash it up to the newer 2.x firmware yet.

Wordpress vs. Luke

Sunday, July 16th, 2006

This blog runs the excellent Wordpress blogging software. In addition, to be nicer to the system it’s hosted on (at Dreamhost), I’ve got the WP-Cache plugin installed (which caches pages so that they don’t have to be regenerated on every page load.)

At the moment I’ve got things running under CGI, which isn’t the most efficient way of doing things (but it’s easy.)

In order to satisfy my own curiosity, as well as squeeze a little bit more performance out, I tried the other day to get this Wordpress install to play nice with PHP running through FastCGI, with the APC PHP opcode cache. I say ‘tried’ because it was a complete and utter failure. APC and WP-Cache definitely don’t like each other one little bit.

Exceptions > Errors

Friday, July 7th, 2006

Today, whilst working with some PHP code, I wanted to catch an error like an exception.

PHP’s errors are a kludgey solution left-over from before PHP had objects. When PHP 5 introduced proper Exception support, I was overjoyed - until I learned that all the existing inbuilt PHP functions would continue to trigger errors rather than throwing exceptions. I understand that it’s necessary for backwards-compatibility, but it still really, really sucks when you want to write clean code.

So, today I was writing my own code to convert errors into exceptions when I hit a frustrating roadblock: the PHP Exception class doesn’t allow you to alter the stack trace. This is a Bad Thing in this case because you must throw the exception inside your error handler - which means that the stack trace starts at your error handling function, making the entire stack trace bloody useless.

Whilst Googling around I happened upon a few snippets here and there about the existence of an ErrorException in the SPL extension of PHP 5.1+. It is a wonderful thing - it’s basically an exception, with the added ability to pass in the stuff passed to a custom error handler, which is then put in the stack trace. It is truly a godsend for debugging.

Anyway, because I have a memory like a sieve and mentions of ErrorException are scarce, here’s the minimal code to use it:

function errorToExeption($errno, $errstr, $errfile, $errline) {
    throw new ErrorException($errstr,0,$errno,$errfile,$errline);
}
set_error_handler('errorToException');

That’s it - now you can treat any function that triggers errors like it throws exceptions instead. You might like to create a couple of subclasses of ErrorException for errors, warnings, etc. and then throw the appropriate exception inside your error handler. Don’t forget that a custom error handler can’t catch every type of error, such as those that might leave PHP in an unstable state, or those that occur inside the Zend Engine before your script is executed.