1

I'm a rookie with MVC and OOP and I'm trying to learn it with tutorials and by trying some stuff out myself. I've got this code:

Model.class.php:

class Model {
    public $string;

    // Add a value to the instance variable $string when object of this class is instantiated
    public function __construct(){
        $this->string = "MVC + PHP = Awesome!";
    }
}

Controller.class.php:

class Controller {
    private $model;

    // For now, the Controller doesn't do anything, because no controlling features have been implemented yet
    public function __construct($model) {
        $this->model = $model;
    }
}

My View.class.php:

class View {
    private $model;
    private $controller;
    private $account;

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

    // Return the value of the $string variable
    public function output(){
        return $this->model->string;
    }
}

This is my index.php where a simple string is echoed, as a result of the code above:

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

// Execute the output() function from the View class to output the text
echo $view->output();

I've created the account class myself and it should output 'Yoo' instead of 'PHP + MVC = Awesome!' which looks like this:

class Account extends Model {    
    // Add a value to the instance variable $string when object of this class is instantiated
    public function __construct(){
        $this->string = "Yoo";
    }
}

When I do return $this->model->string; it correctly outputs PHP + MVC = Awesome!. But when I do return $this->account->string, it outputs nothing. No errors or anything. Why isn't this working?

1
  • 3
    In the constructor for your View, you have $this->account = $account, but $account is undefined in that function. Commented May 19, 2015 at 16:16

1 Answer 1

2

This answer is just a basic example of MVC


How a complex MVC framework functions

In very big lines

In fact your index file should only run an Application class or something that will route the request to the good controller and method. But, well, if you want to take a start, you don't need to make something very complex.


Your error

(In my opinion)

Here your main error is thinking that it's your index file that call model, then pass it to controller which you will pass to view. In MVC the controller should be able to do all of this himself, once called.


How it works

Still in big lines

Index file instantiate a controller, then call a method of this controller. The controller call the model, that gather data and send it back to controller, which will send this data to the good view (still called by controller), then the view will display the result.


Example

index.php

<?php
// i have only one controller, with only one action, so i call it.
// i havnt autoloader so i do all requires here

require_once 'MyController.php';
require_once 'MyModel.php';
require_once 'MyView.php';

$Controller = new MyController();
$Controller->exampleAction();

MyController.php

<?php
class MyController
{
    public function exampleAction()
    {
        $model = new MyModel();
        $view = new MyView();
        $view->send($model->data());
    }
}

MyModel.php

<?php
class MyModel
{
    public function data()
    {
        return 'Some data from the model';
    }
}

MyView.php

<?php
class MyView
{
    public function send($data)
    {
        echo $data;
    }
}

Go further

By the way, the controller can have a dedicated model, then you can load it in his constructor

MyController.php

<?php
class MyController
{
    protected $Model;

    public function __construct()
    {
        $this->Model = new MyModel();
    }
    public function exampleAction()
    {
        $view = new MyView();
        $view->send($this->Model->data());
    }
}

And same for view.


Hope this answer helped you to understand how MVC works ^^

Sign up to request clarification or add additional context in comments.

1 Comment

Very good answer, thanks! I will study your comment and continue from there. Your explanation clears up a lot of confusions I had.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.