mercredi 4 novembre 2015

Database connection: best design approach php

I'd like to know what would be your approach in this case:

The language is PHP.

We have a base class BaseObj, which is the base class of the "business objects" (member, employment...). And within it there are the dabase methods (get, insert, update...). It wasn't created a DAO (Data Access Object) at first, and now it is not an option to create it and separate the object from its database methods, for other big reasons that I think it is not necessary to explain here...

The db connection is encapsulated into the class DBManager, and therefore we need to provide it to our BaseObj. To keep alive only one DBManager instance per request, we are providing it to the BaseObj through dependency inject on its constructor. But them sometimes we instantiate the BaseObj with no intent run database related methods, and so we have to provide the database anyway to instantiate the BaseObj.

Question: Would it be a better approach to provide the database to the BaseObj through a method, lets say setDBManager method, and check if there is a DBManager in all the BaseObj's database related methods? Or what would be a better design you'd follow?

Aucun commentaire:

Enregistrer un commentaire