DeX Posted June 9, 2022 Share Posted June 9, 2022 I have models for all of my database objects. For instance, I have a User class and in that class I have functions like so: getFirstName() getLastName() getAddress() And of course I have setters for each as well. My question is whether I should also have some helper functions inside this model for something like getFullName() which will return a concatenation of getFirstName() and getLastName() or if we should simply concatenate the data every time we want to display it on each page. So we will be displaying the user's name on the top of each page, in their account profile and maybe some other places of this portal type system. Is it proper object oriented programming to use helper functions like this? Should they go into the controller? Should we create separate logic namespaces for each model to control this logic? Another example would be if I wanted to get the most recent user that signed up, where would this function go? In the user model? In the controller? Thanks. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 10, 2022 Share Posted June 10, 2022 1 hour ago, DeX said: My question is whether I should also have some helper functions inside this model for something like getFullName() which will return a concatenation of getFirstName() and getLastName() Does it make sense for the model to have a method related to its data? 1 hour ago, DeX said: or if we should simply concatenate the data every time we want to display it on each page. Does it make sense to do the same thing over and over again? 1 hour ago, DeX said: Is it proper object oriented programming to use helper functions like this? Should they go into the controller? Should we create separate logic namespaces for each model to control this logic? Code belongs in the one, single place (hopefully) that it is most related to. If the name is related to the model then it should be the model that deals with the name. If code is related to the controller, that being the place where you have code to deal with requests and responses and deciding what to render, then that is where it should go. 1 hour ago, DeX said: Another example would be if I wanted to get the most recent user that signed up, where would this function go? In the user model? In the controller? Is finding the most recent user something that relates mostly to the data managed by your model or to process managed by the controller? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.