Creating reusable modules with database access

I’m wondering what the best practice would be if I wanted to create modules (classes) that are as framework agnostic as possible. For example, I have a module (a group of classes) to send out mailing messages, or a module to accept and manage patient registrations. I have clients who often have a web site or an online application and ask me to add some functionality to their existing system. Each of these applications have been based on a different framework so it would be useful for me to be able to reuse my classes in those applications regardless of the framework.

In most cases I am able to create classes that are pretty independent from the outside world, however the biggest problem is the database access. Each site/framework uses a different way of accessing the database, they have their own abstraction layers or wrapper functions or classes based either on PDO or mysqli. This makes it hard for my modules to be independent because in each case the interface for database access is different.

How would you go about this? Create a proxy class (db driver) that would sit between my module and the external system? I imagine it would be advisable to use the same database connection as the underlying system so as not to create another connection just to be independent in my programming style.

I don’t know which frameworks we’re talking about here, but wouldn’t it be possible to just get the PDO instance from the framework and pass to your class so you’re going to the lowest level possible?

If that’s not possible, or you don’t want to do that, than yes, I think a db driver would be your best bet.

Currently, I’ll have to deal with an older version of CodeIgniter but sometimes a project is based on WordPress, Kohana or some other custom made system.

Getting PDO instance would be best but sometimes PDO is not used at all. But this seems like a good idea, I could either get the PDO or mysqli instance, if this is possible then writing drivers just for these two is still much less work than writing for each framework separately.