2

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! :)

4
  • Is there any error in error log? Commented Nov 6, 2015 at 10:17
  • Hy, there are some warning: When doing "update": pastebin.com/cxnLs4y2 - and when doing "preview": pastebin.com/w3vvLAjB - but the update is working: learn.berndklaus.at/test1 Commented Nov 6, 2015 at 10:25
  • Oh it's just a notice complaining that your $a variable is uninitialized when appending for the first time. This line $a .= mt_rand(0,9); Commented Nov 6, 2015 at 10:37
  • Little tips : the good practice is to call your model from controller and never from the view, you will have to move the getModel function to respect MVC separation. the idea is to provide data in the controller, manage everything and send raw data prepared to the view which are displayed directly. Commented Nov 6, 2015 at 11:09

1 Answer 1

2

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();
            }
Sign up to request clarification or add additional context in comments.

2 Comments

Hy, thanks that's a good input! So, in normal cases i would save this to a database (but i didn't cause i just want try the code), and if it's saved to a DB i wouldn run into this issue?
That's correct, you wouldn't have to use sessions for this purpose in that case. I think you should now try to integrate PDO into your model.

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.