1

I have a problem working with MVC patterns; I created multiple models, I'm using each one of them to handle grouped tasks(connection, DB, ect..).

Problem is, after creating the basic models, I ended up with code like this(within the controller).

class Controller 
{
    function doStuffz() 
    {
        $accounts = new AccountsModel();
        $DB       = new DBModel();
        $HTML     = new HTMLModel();
        //More models...

        $accountData = $accounts->getAccount($id);
        $DB->saveAccount($accountData);
        $HTML->display($accountData);
        //More code...
    }

}

I've been creating instances of models within (almost) all functions in my classes, even though it seems absolutely fine to make them static.

I read this questions about when to use static methods, after applying the "rule-of-thumb" it seems all of these models should be made static, is that a bad practice in MVC patterns?

If I make these models static, I'll probably end up with something like this:

class Controller 
{
    function doStuffz() 
    {

        $accountData = AccountModel::getAccount($id);
        DBModel::saveAccount($accountData);
        HTMLModel::display($accountData);
        //More code...
    }

}

Which actually makes more sense to me, also it looks much cleaner and shorter.

Community
  • 1
  • 1
Abdulaziz
  • 2,166
  • 1
  • 19
  • 35

1 Answers1

0

DBModel, HTMLModel are not actually models, they are services.

Using static way is convenient, but it has disadvantages: global scope, tight dependes, hard to test.

sectus
  • 14,914
  • 5
  • 49
  • 88
  • How do they differ from models? I mean, I've seen classes titled as "models" that does exactly like DBModel(running queries.) – Abdulaziz Mar 25 '13 at 02:10
  • @AzizAG, ok. You catched me :^ ). I do not know how to really separate service and models. I like this [question](http://stackoverflow.com/questions/5702391/mvcs-model-view-controller-service). – sectus Mar 25 '13 at 03:25
  • Whether the class is a service or a model, it should be static. Fair enough. – Abdulaziz Mar 26 '13 at 02:41
  • So, you will get your disadvantages. – sectus Mar 26 '13 at 02:42
  • Regarding the "is it a model or a service?" question: it's a conceptual question rather than one that can be answered just by looking at the code. Services are classes that "do stuff for you" while models are classes that "store data about and/or represent a specific type of thing" (usually you'll have an almost one-to-one mapping of database tables to model classes). – Aziraphale Jul 19 '14 at 14:57