tags:

views:

331

answers:

8

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. :-)

+1  A: 

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.

GaryF
A: 

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.

ggasp
A: 

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 ?

Pat
A: 

@Pat: Good question! I have no idea where those titles came from, certainly not me. I'll look into it more later, and possibly file a bug.

GaryF
A: 

@Pat, @ggasp: It's not an abstraction of the database function calls that I'm after (I'm not planning on moving DB), it's an abstraction of the storage of objects into the database, like this. That usually does involve a database access abstraction too, which is handy, but this sits on top of that. Doctrine does look promising, so far - just need to find a little time to play. My 'framework' objection was more aimed at bigger things like Cake, where you have to buy into MVC too - I want to be able to gradually migrate to this thing.

AnotherHowie
A: 

@GaryF - Doctrine isn't really a "framework" is it? It's an ORM, and definitely a big one, but when I think of frameworks I think of Symfony or CakePHP or CodeIgniter. Doctrine is really great and getting better all the time.

AdamTheHutt
A: 

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.

Jack M.
+1  A: 

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.

alan szlosek