Monday, June 26, 2017

Addition of Two Numbers Using Model View Controller in PHP

A program that demonstrate how to use the MVC or Model View Controller in PHP to add the sum of two numbers the code is very short and easy to understand. Thank you for visiting the site.

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

<?php
/**
 * Created by PhpStorm.
 * User: Jacob Pomperada
 * Date: 6/26/2017
 * Time: 8:24 AM
 */


class Model {
    public $a;
    public $b;

    public function __construct() {
        $this->a=5;
        $this->b=5;
    }
}

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 "<span style=\"font-size: 20px !important; font-weight:bold; font-family: arial; color: blue;\">";
        echo "Sum of Two Numbers Using Model View Controller Concepts in PHP </span>";
        echo "<br><br><br>";

        $sum = ($this->model->a + $this->model->b);

        $c = $this->model->a;
        $d = $this->model->b;

        echo "<span style=\"font-size: 20px !important; font-family: arial; font-weight:bold; color: blue;\">";
        echo " The sum of $c and $c is $sum. </span>";
        echo "</font></body>";
    }

}

class Controller {
    private $model;

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

}

$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