6

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.

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.