Electricmonk

Ferry Boender

Programmer, DevOpper, Open Source enthusiast.

Blog

PHP

Thursday, July 8th, 2004

Now that I’ve taken the job at ZXFactory, I’ll be doing some heavy-duty PHP programming again. It’s been a while since I last programmed in PHP. (Not that I’ve forgotten how to do so). I did a little research for some functional specifications I am working on at ZX and found some interesting sites and articles.

PEAR DB

The project I’m currently working on will probably be put on the market, so it was decided that we should start looking for some database abstraction library. The idea is that you should be able to change the database back-end from i.e. MySQL to Postgresql without having to change any code.

So I started looking around and ended up at the PEAR repository. It has some nice database abstraction layers like PEAR::DB. DB provides an Object Oriented layer around the database call-functions. It unifies things like the various different functions for counting the number of affected rows so that it will work on any database. It also restructures the results from queries so that you won’t have to deal with different types of resultsets. Best of all are probably the standard error reporting routines.

The PEAR::DB_DataObject_FormBuilder is also pretty cool.

PEAR::DB_DataObject‘s description sounds really cool (“An SQL Builder, Object Interface to Database Tables”), but isn’t really usefull. It’s just a kind of template for building classes for retrieving data from tables.

What you really want (yes you do!) is a unified way to write queries. You see, the problem with different database backends is that you can’t use any of the database vendor specific functions and/or syntax. And this really limits the possibilities. I haven’t really found any useable way of doing this, so you’ll probably just have to stick with ANSI-SQL and do any complicated things in the code.

PHP’s DBX

If you don’t like PEAR, you could always use PHP’s native DBX library. It’s faster than PEAR’s stuff (if you believe these statistics) and you won’t have to fiddle around with PEAR installations. Here are some DBX examples.

Shared memory

Another cool feature in PHP is the POSIX Shared memory PHP library. Suppose you have to retrieve some statistics from a database, do some calculations on them and show the endresult to users visiting the page. Let’s say you’ve got 8 million rows in the database and you have to add the values of some column of all those rows. That’s going to take some time. You most certainly don’t want to do that every time a user visits the page now, do you? The awnser to this problem is probably shared memory.

With shared memory you could simply perform the calculations once and then allocate some shared memory (the size of the results) and put the result in there. Then, when another user visits the page, you can first query the shared memory to see if the results are already calculated. If so, you can simply retrieve the results from memory without having to perform any queries or calculations at all. Pretty neat stuff. Here’s an article on how to use them.

Debugging

Did you know you can produce tracebacks in PHP? You could write your own errorhandler function which shows the traceback.

PHP 5

On a final note, I’m eagerly awaiting the release of PHP 5. Check out this new stuff. Lot’s of great new Object Oriented features like public, private and protected vars/methods, interfaces, class type hinting and best of all: Overloading! Too bad they’re also implementing exceptions. I hate exceptions.

The text of all posts on this blog, unless specificly mentioned otherwise, are licensed under this license.