Question

I have inherited an old crusty PHP application, and I'd like to refactor it into something a little nicer to deal with, but in a gradual manner. In perl's CPAN, there is a series of classes around Class::DBI that allow you to use database rows as the basis for objects in your code, with the library generating accessor methods etc as appropriate, but also allowing you to add additional methods.

Does anyone know of something like this for PHP? Especially something that doesn't require wholesale adoption of a "framework"... bonus points if it works in PHP4 too, but to be honest, I'd love to have another reason to ditch that. :-)

Was it helpful?

Solution

It's now defunct but phpdbi is possibly worth a look. If you're willing to let go of some of your caveats (the framework one), I've found that Doctrine is a pretty neat way of accessing DBs in PHP. Worth investigating anyway.

OTHER TIPS

Class::DBI is an ORM (Object Relational Mapper) for perl. Searching for "PHP ORM" on google gives some good results, including Doctrin, which I've had good luck with. I'd start there and work your way up.

I'm trying to get more feedback on my own projects, so I'll suggest my take on ORM: ORMer

Usage examples are here

You can phase it in, it doesn't require you to adopt MVC, and it requires very little setup.

The right thing to is to access the database via an abstraction layer in a way such if you change your RDBMS or how you implemented that access, you only have to modify this layer while all the rest of your application remains untouched.

To do this, to free your application from knowing how to deal with the database, your abstraction layer for DB access must be implemented by a framework such as ADODB.

All the files related to this layer must be located in a sub directory:

  • /ado

In this directories you'll put all of your .php.inc files which contains general methods to access the database.

How about MDB2 from pear?

It provides a common API for all supported RDBMS. The main difference to most other DB abstraction packages is that MDB2 goes much further to ensure portability.

Btw: @GaryF what are those strange title attributes your links have ? Did you add them or are they added by SO ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top