一个简单的应用MVC思想的php程序

发布时间 2023-05-22 16:21:45作者: paoPaoLong_liu
<?php
//controller.php  控制器程序

require_once("model.class.php");
$type = isset($_GET['type'])?$_GET['type']:3;

$modelObj = new DateTime2();
    switch($type)
    {
        case 1:
            $str = $modelObj->getDate();
            break;
        case 2:
            $str = $modelObj->getTime();
            break;
        default:
             $str = $modelObj->getDateTime();
    }

include "./view.html";
?>
<?php
//模块库程序 model.class.php class DateTime2 { public function getDate() { // return '1'; return date('Y-m-d'); } public function getTime() { // return '2'; return date('H:i:s'); } public function getDateTime() { // return '3'; return date('Y-m-d H:i:s'); } } ?>

  视图文件 view.html 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MVC思想简单演示</title>
</head>
<body>
    <a href="?type=1">显示日期</a>|
    <a href="?type=2">显示时间</a> |   
    <a href="?type=3">显示日期时间</a>
    <h2>当前是:<font color='red'><?php echo $str ?></font></h2>
    <h3><?php echo 'ByeBye!' ?></h3>
</body>
</html>