作业内容:将MVC3目录中的内容,按自己理解再写一遍
//入口文件
<?php
namespace hw;
require __DIR__ . '/Model.php';
require __DIR__ . '/View.php';
require __DIR__ . '/Controller.php';
$m = (new Model('mysql:dbname=testsql','zolo','123456'));
$v = new View();
echo (new Controller($m,$v))->index();
//controller
<?php
namespace hw;
class Controller{
protected $model;
protected $view;
public function __construct($model,$view)
{
$this->model = $model;
$this->view = $view;
}
public function index(){
$data = $this->model->selectAll(15);
$this->view->display($data);
}
}
//model
<?php
namespace hw;
use PDO;
class Model{
protected $db;
public function __construct($dsn,$username,$password){
$this->db = new PDO($dsn,$username,$password);
}
public function selectAll($argc){
//字符串转数字
$num = number_format($argc);
$s = $this->db->prepare("select * from `users` limit $num");
$s->execute();
return $s->fetchAll();
}
}
// $m = (new Model('mysql:dbname=testsql','zolo','123456'))->selectAll(5);
// print_r($m);
//view
<?php
namespace hw;
class View{
public function display($argc){
// 1. 模型赋值
$users = $argc;
// 2. 渲染模型
include 'show.php';
}
}