Berndinox
Berndinox

Reputation: 2414

PHP - What is wrong? Learning MVC - Beginner

Hy,

i started learning PHP and i created a simple MVC Style Codebase. The Script just generates a random number and displays this numer. I also write a function to display the number shown before but it does not work. The value is empty. Can you help me out, i have no clue whats wrong and there is no php error thrown.

view.php

<?php

class View
{
    private $model;
    private $view;

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

    public function output()
    {
        echo 'Current Entry: ';
        echo $this->model->getData();
        echo '<br />';
        echo '<a href="?action=update">Update</a>';
        echo '<br />';
        echo '<a href="?action=preview">Last</a>';
    }

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

}

controller.php

<?php
class Controller
{
    private $model;
    private $view;

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


    public function get($request)
    {
        if (isset($request['action']))
        {
            if ($request['action'] === 'update')
            {
                for ($i = 0; $i<6; $i++) 
                {
                    $a .= mt_rand(0,9);
                }
                $this->model->setData($a);
            }
            elseif ($request['action'] === 'preview')
            {
                $this->model->setLast();
            }
            else
            {
                $this->model->setData('Wrong Action');
            }
        }
        else
        {
            $this->model->setData('Bad Request');
        }
    }
}

model.php

<?php
class Model
{
    private $data;
    private $last;

    public function __construct()
    {
        $this->data = 'Default';
    }

    public function setData($set)
    {
        if ( ! (($set == 'Wrong Action') && ($set == 'Bad Request')))
        {
            $this->last = $this->data;  
        }
        $this->data = $set;
    }

    public function getData()
    {
        return $this->data;
    }

    public function setLast()
    {
        $this->data = $this->last;
    }

    public function getLast()
    {
        return $this->last;
    }
}

index.php

<?php

require_once 'controller.php';
require_once 'view.php';
require_once 'model.php';

$view = new View();
$controller = new Controller($view);


if (isset($_GET) && !empty($_GET)) {
    $controller->get($_GET);
}



$view->output();

Are there any other, bad mistakes in the Script? Any input very welcome! :)

Upvotes: 2

Views: 112

Answers (1)

Stas Parshin
Stas Parshin

Reputation: 1703

The problem with your code is that PHP does not preserve variable values between requests, therefore, when you set your $model->last value here:

$this->last = $this->data;

It gets reset on your next request.

You may want to store $last value in a session or a cookie instead. Something like:

$_SESSION['last'] = $this->data;

And then when you are instantiating your model you could initialize it with a value stored in a session if available:

index.php - add session_start() at the beginning

model.php:

public function __construct()
{
    $this->data = isset($_SESSION['last']) ? $_SESSION['last'] : 'Default';
}

public function setData($set)
{
    $this->data = $set;

    if ( ! (($set == 'Wrong Action') && ($set == 'Bad Request')))
    {
        $_SESSION['last'] = $this->data;
    }
}

controller.php

  elseif ($request['action'] === 'preview')
            {
                //Remove this
                //$this->model->setLast();
            }

Upvotes: 2

Related Questions