7

I'm trying to learn how the MVC pattern works so have been playing around a bit. I just wrote this, which was making sense to me, until I wanted to display the array content which has been collect by the Controller class via the View class output() method.

<?php 

class Model {

    public $arrayPeople = array(
        1 => 'Dave',
        2 => 'Burt',
        3 => 'Jack',
        4 => 'Michael',
    );

    public $arrayAnimals = array(
        1 => 'Fish',
        2 => 'Cat',
        3 => 'Dog',
        4 => 'Tiger',
    );

}


class Controller {

    private $model;

    public function __construct( $model ){

        $this->model = $model;

    }

    public function getArray( $arrayName )
    {
        return $this->model->$arrayName;
    }

}


class View {

    private $model;
    private $controller;

    public function __construct( $model, $controller ){

        $this->model = $model;
        $this->controller = $controller;

    }

    public function output()
    {
        echo '<a href="?action=getArray&value=arrayAnimals">arrayAnimals</a><br/>';
        echo '<a href="?action=getArray&value=arrayPeople">arrayPeople</a><br/>';
    }


}


$model = new Model;
$controller = new Controller( $model );
$view = new View( $model, $controller );


if (isset($_GET['action']) && !empty($_GET['action'])) {
    $controller->{$_GET['action']}($_GET['value']);
}


echo $view->output();

Update I just found this online, which seems to suggest the View should 'pull' the data from the Model?

class View
{
    private $model;

    public function __construct($model) {
        $this->controller = $controller;
        $this->model = $model;
    }

    public function output(){
        $data = "<p>" . $this->model->tstring ."</p>";
        require_once($this->model->template);
    }
}

This is confusing as Paul Hegarty on iTunes U – 'Developing Apps for iOS' (lecture video 1), he says the Model and View should never communicate and the information flow should always travel via the Controller? But the example about shows direct communication between the View and Model.

MikeMason
  • 319
  • 2
  • 7

1 Answers1

9

Model View Controller is one of the oldest if not first design patterns. It was created at a time when few people understood any proper way for objects to interact. Consequently the only common thing implementations have is three separate areas of responsibility: Model, View, and Controller.

How they interact and which knows about what is completely up for grabs as you'll find if you do a google image search for "model view controller"

enter image description here

Does this mean the connections don't matter? NO! It means Model View Controller is something medical doctors call a "junk term". Sure it sounds like you've told me something significant when you tell me that, but actually, you've told me very little.

It didn't start this way. It used to look like this:

enter image description here
video

Since then it's been twisted into all sorts of knots, to the point where every student who tries to study MVC runs into conflicting information.

When I hear someone say they use model view controller the first thing I ask is what knows about what. Why? Well if the controller doesn't know the view exists you're free to swap out views without touching the controller. If you must talk to it, do so through an interface then at least nothing will care when it gets changed.

Before you succumb to the chaos I suggest you study the observer pattern. It teaches you what an event really is. This allows a very different kind of communication that some of these diagrams represent in different ways (an outlined arrow, dotted line, notify, etc). It won't unify these diagrams but they might make a little more sense.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • Hi, candied_orange. Would you please be so kind to change this sentence, so that we clearly, without a doubt, understand what you meant?: _"If you must talk to it, do so through an interface then at least nothing will care when it gets changed."_. Unclear is: who is _"you"_ and which is _"it" _ (twice used). Also, which MVC implementation are you suggesting for an MVC-based web application, which could grow (very) big? –  Oct 14 '21 at 04:03
  • My preferred implementation: _Controller_ updates _Model_ (which is a layer), _View_ fetches needed data from the _Model_ layer and presents it to the user (browser). _Controller_ and _View_ don't know about each other. They share service(s) of the _service layer_ (which depend on _Model_ layer components: data mappers or repositories, entities, etc). Thank you very much. –  Oct 14 '21 at 04:11