Web Development

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.

Doing too many things at once

Tuesday, February 21st, 2006

Why oh why do I persist in trying to do waaay too many things at once?

At the moment I’ve got three web sites under development. Only one of those (freewebhosters.info) is currently online, and as it is it’s terrible and doesn’t get any traffic because of that. Practical Pupil, my student organiser site, is taking a long time mainly because I can’t make up my mind as to how to do things.

And to top it all off, I’m forever suffering from attacks of procrastination and indecision. Gah.