Home » Php » php – Can a utility class be a Model class in a MVC framework?

php – Can a utility class be a Model class in a MVC framework?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Im designing a MVC framework but I dont know where should I put my utility classes. I read somewhere where it said utility classes are Model Classes. Is this line of thinking correct or are they some separate type of class?

How to&Answers:

It depends on what these Utility classes are doing. The main idea of MVC is to separate the presentation layer (V and C) from the remaining application (M). If your Utility classes handle things related to the presentational layer, place them in an appropriate package, for instance ViewHelpers or InputHandlers. If the Utility functions relate to anything else, see to which layer in your model they best fit into, for instance DataAccess Utilities go into the DAO package while a a MailHelper might go into the the Services package, and so on.

Answer:

Model class can contain your data access code, application logic. so it is okay to place in Model.

The best approach is to create a folder Utility in Model folder and place utility class there.

Answer:

If it isn’t a representation of stored data, then it’s probably not a Model class. Even then, if it still represents stored data of some sort, but behaves radically different from the rest of your Models, it may not belong with the rest of the Models, anyway.

Utility functions can be part of the whichever class they are utilities to, either Model, View or Controller. If they’re a bit more generic, then they kind of live “outside” the MVC design.