Archive for August, 2006

PHP Class Autoload

Thursday, August 31st, 2006

It’s been a long time since I posted.  I just wanted to write up a quick post today to say how much I like the new __autoload() function in PHP.  For those not familiar with it, the function gets called when a class is used in PHP that has not been defined.  This gives the developer a chance to allow the script to find the include file it needs and include it to define the class.

The function’s not all that new, but I’m just getting around to using it and it’s changing the way I design my sites.

So now I don’t use ever need to manually include a file anymore.  I don’t use any global functions.  I make all my otherwise-global functions static functions of some class and call them that way.  I never have to remember to include the file at the top of the page, I never have to worry about including it twice because of the quirks of include_once(), and I don’t have a bunch of orphaned include()s at the top of the script anymore from removing a function but leaving it’s include.

I’m sure there’s someone out there that thinks this isn’t a good idea for whatever reason.  I’m all ears, but for now, this has made my PHP development much easier.