Search code examples
oopdatabase-designorm

Should I have a separate class for each database table?


I assumed this was good practice, for reasons such as it helps to clearly represent relationships between classes. However, according to this website it's not:

http://www.tonymarston.net/php-mysql/good-bad-oop.html#a5

So, is having a class for each database table good OO practice or not?


Solution

  • Briefly reading the article in the introduction these points are said to 'complaints' against another article the author had written. I believe the author is actually saying that a class per table is a good approach and explains why, defending it against remarks such as "This code leads to a problematic dependency between the DB and the class".

    Having a table per class is certainly a good place to start from, however like any pattern it is not necessarily going to be something that'll be required in all situations. An example being a junction table where perhaps just one class can handle interactions for numerous different junction tables. Likewise you may have tables with a one to one relationship that fits the program better as a single class.