DEV Community


Discussion on: Creating a Database Connection As Far As We Obey to OOP

aleksikauppila profile image
Aleksi Kauppila

Database appears to be singleton, but it's possible to freely create instances of Database via public constructor.

IconnectionInfo provides getConnection() which returns a PDO object but Database also provides getSelectQueryResult()-method for results.

So it seems there's one responsibility too much. Do you want to provide users with database connections or get them results from the database? (Also, i think you should move the code from the constructor inside getConnection().)

Whichever responsibility you choose, i feel it would be more beneficial to provide a Database interface. This interface could be implemented by classes like PostgreSQLDatabase, MySQLDatabase, SQLiteDatabase etc. These implementations could take connection information as arguments because hard coding them is a bad practice.

Interfaces shouldn't be used as configuration. They should have no direct effect on the implementations internals.