I'm going to take the example of a user
object. A user needs to be registered, logged in, logged out, edited (e.g., email change), etc.
So on one hand I have a user
object, which includes a variety of class variables (pseudo, email, etc) along with getters and setters and maybe some functions that do not deal with the db.
On the other I have a DAO
class which is the object that directly deals with the database through a variety of MySQL / PDO queries (create record, update, retrieve info, etc).
Is there any reason to not have the user
object interact directly with the DAO
object? In other words, when the Controller
request a database query relating to an existing user
instance (e.g., during the registration process), should it simply call a function in user
which itself calls a function in DAO
, or should there be a layer in between?
I have seen examples where the controller calls a 3rd class to interact with the DAO, and passes a user
instance as an arg. Sometimes instead, this third layer is in charge with creating the user
instance and dealing with the DAO
. It seems to me that all the functions used to deal with the DAO
could reside inside the user
object. What am I missing?