Monday, June 26, 2017

Hello World Using Model View Controller Concept in PHP

Here is a program that I wrote using PHP to demonstrate the concept of MVC or Model View Controller this program will display hello world message on the screen. The code is very easy to understand and use. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing

hello.php

<?php

class Model {
    public $text;

    public function __construct() {
        $this->text = 'Click Me';
    }
}

class View {
    private $model;

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

    public function output() {
        echo "<body bgcolor='lightgreen'>";
        echo "<font-face='arial'>";
        echo "<br><br>";
        echo "<h1> Hello World Using Model View Controller Concepts in PHP </h1>";
        echo "<br>";
        return '<a href="demo.php?action=pressme">' . $this->model->text . '</a>';
        echo "</font></body>";
    }

}

class Controller {
    private $model;

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

    public function pressme() {
        $this->model->text = 'Hello World Mr. Jake Pomperada !!!!';
    }
}


$model = new Model();
$controller = new Controller($model);
$view = new View($model);
if (isset($_GET['action'])) $controller->{$_GET['action']}();
echo $view->output();
?>


No comments:

Post a Comment