I have a chicken-egg problem. I would like too implement a system in PHP in a OOP way, in which two classes would play important roles: Database and Log. My idea was to build up the connection by the Database class, which would have public methods eg. runQuery(sUpdateQuery), doInsert(sInsert), etc. The Log class would writing logs through common methods just as like logMessage(message), logDatabaseQuery(sQuery) TO THE DATABASE. The problem comes now.
1: Inside the methods of the Database(s class I would like to be able to use the Log class(s logDatabaseQuery(sQuery)
2: This still would not be a big challenge if I would not like to use the Database class(s doInsert(sInsert) method inside the logDatabaseQuery method.
I would like to keep it simple - and just use one instance of the database connection object, and from the loggeras well, if it is possible.
For many people Singleton model would be the first idea to choose, but I definitely would like to use different solution.
So there would be two classes which would use each-other(s methods:
I would like to keep the Log methods separately (in the Log class), since later on it would have other methods for logging not just to the database, but to files or e-mails as well.
Any ideas, how this should / could be done in the nicest, OOP friendly way?
I was thinking about a common parent abstract class, or about using interfaces as well, but finally could not figure out the proper way :(
What I would like to know is a suggestion for a correct class hierarchy
posted date: 2009-04-07 13:22:00