One way.. or another..

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

One way.. or another..

Post by Jenk »

(Apologies for vague title.. it just so happened I was listening to Blondie at the time..)

I'm unsure which way my application should operate when it comes to View <-> Model.

Should my View Controller/Class ask for the page variables from the Model, or should the Model feed them to the View?

E.g.:

Code: Select all

$model = new Model(new View);
vs:

Code: Select all

$view = new View(new Model);
should it be the former and my View class implement:

Code: Select all

interface iView
{
    public function assignVariable ($var, $val);
    public function setTemplate ($template);
}
or, the latter, all my model classes will implement:

Code: Select all

interface iModel
{
    public function getTemplate ();
    public function getVariables ();
}
I'm at a bit of a cross roads with it, because in all other scenarios, it's always take, not give, but with my view <-> model interaction, it's give..

It works, but it's bugging me.. I like uniformity but also I like to meld with 'the norm', and as I need to revamp one of my projects this would be an ideal time to make this change, however being as indecisive as I am.. what do you guys think?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

My personal preference is to pass the model to the view, so I end up with something like

Code: Select all

class IndexView extends ApplicationView
{
   function __construct($model = false)   
   {
      if ($model)
      {
         $this->model = $model; 
      }
   }

   function setNewBlock($max)
   {
      $this->set('latestnews', $this->model->getNewsTitles($max));
   }
}
I know a lot of people prefer to have the controller directly interacting with the model, but this way I can throw in some hidden logic that doesn't belong into the controller.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Definitely pass the Model to the VIew. This is the most important dependency to get right in programming. The View is in the Presentation Layer and the Model is in the Domain Layer. You want to fight to eliminate/minimize the Domain Layer having any dependencies on the Presentation Layer. Likewise the Data/Service Layer should not have any dependencies on the Domain Layer.

There are rare exceptions, and I would recommend implementing a Helper object to do that if necessary to document what is going on.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I tend to think the View depends on the Model: if the View changes, the Model shouldn't have to change. ;-)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Thanks guys, and JCart's example has totally opened my eyes in realising that the View doesn't have to be the same class for every page/action.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

right.. so now I'm using classes as per similar to JCart's example..

I have a base View class with the template engine assignment, and Model assignment, variable assignment, template assignment yada yada...

I then extend with a sub-class that is page/action specific for generating the content and loading into the template engine..

For the model, I have a base class for DB object assignment and User object assignment, and again extend with sub-classes specific for each page/action.

It's all so clear all of a sudden.
Post Reply