总结:M V C 三个页如果不使用命名空间和自动加载来实现,很容易实现普通方法的依赖注入 和构造方法的依赖注入。
如果按目录结构使用命名空间和自动加载后,可以在MVC状态下实现普通方法的依赖注入,在构造方式注入时会报参数错误,目前未查到原因,如下 致命错误:未捕获的类型错误:参数1传递给 lib\controller\Controller::__construct() 必须是的实例lib\controller\Model,给定的lib\model\model的实例。。。
去掉命名空间以及自动加载实现的MVC 依赖注入:
一、View.php 代码
实例
<?php /** * Created by PhpStorm. * User: A * Date: 2019-08-07 * Time: 1:58 * View: 视图类 */ Class View { public static function get(){ echo "View类自动加载正常"; } public $rows = null; public $pages = null; public $page = null; public function viewHtml($rows, $page, $pages) { $this->rows = $rows; $this->page = $page; $this->pages = $pages; $upPage = (($this->page - 1) == 0) ? 1 : ($this->page - 1); $nextPage = (($this->page + 1) > $pages) ? $pages : ($page + 1); $html = <<<STR <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> table,th,td{ border:1px solid #000000; } table th{ background:lightblue; } table{ border-collapse:collapse; width:600px; margin:0 auto; text-align:center; } tr{ line-height: 24px; } h3{ text-align:center; font-size:16px; } h3 a{ text-decoration-line: none; margin-left: 10px; margin-right: 10px; padding:0px 2px 0 2px; border:1px solid #000000; display:inline-block; min-width:50px; height:28px; line-height: 30px; background:lightgreen; } a:hover{ color:#ffffff; } </style> <body> <form action=""></form> <table> <caption>员工信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>工资</th> </tr> STR; foreach ($this->rows as $val) { $html .= "<tr>"; $html .= "<td>" . $val['id'] . "</td>"; $html .= "<td>" . $val['name'] . "</td>"; $html .= "<td>" . $val['age'] . "</td>"; $html .= "<td>" . $val['sex']. "</td>"; $html .= "<td>" . $val['salary'] . "</td>"; $html .= "</tr>"; } $html .= "</table><h3><a href='controller.php?p=1'>首页</a>"; $html .= "<a href='controller.php?p={$upPage}'>上一页</a>"; // 分页码 for ($i = 1; $i < $pages; $i++) { $html .= "<a href='controller.php?p=$i'"; if($i == $this->page){ $html .= " style=background:lightblue color=#ffffff;"; }; $html .= ">".$i."</a>"; } $html .= "<a href='controller.php?p={$nextPage}'>下一页</a>"; $html .= "<a href='controller.php?p={$this->pages}'>尾页</a>"; $html .= "</h3></body></html>"; return $html; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
二、Model.php 代码
实例
<?php /** * Created by PhpStorm. * User: A * Date: 2019-08-07 * Time: 3:31 */ class Model{ protected $table = null; protected $list = null; protected $pdo = null; public static function get(){ echo "Model类自动加载正常"; } public function __construct($table,$list) { $this->table = $table; $this->list = $list; $this->pdo = new \PDO("mysql:host=127.0.0.1;dbname=php","root","root"); } public function getPage() { $page = isset($_GET['p']) ? $_GET['p'] : 1; return $page; } public function getDate($page) { $page = isset($_GET['p']) ? $_GET['p'] : 1; $offset = ($page - 1) * $this->list; $sql = "SELECT * FROM `{$this->table}` LIMIT {$offset}, {$this->list}"; $stmt = $this->pdo->prepare($sql); $stmt->execute(); $rows = $stmt->fetchALL(\PDO::FETCH_ASSOC); return $rows; } public function getPages(){ $sql = "SELECT COUNT(*) FROM `{$this->table}`"; $stmt = $this->pdo->prepare($sql); $stmt->execute(); $total = $stmt->fetchColumn(); $pages = ceil($total/5); return $pages; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
三、Controller.php 代码
<1> 普通方法依赖注入
实例
<?php Class Controller{ //*************************************** //普通方法依赖注入 public function index($model,$view){ $rows = $model->getDate(1); $p = $model->getPage(); $pages =$model->getPages(); echo $view->viewHtml($rows,$p,$pages); } } //****************************************** //导入类文件 require "./../model/Model.php"; require "./../view/View.php"; //****************************************************** //普通方法中进行依赖注入 $controller = new Controller(); $data = new Model("staff",5); $view = new View(); echo $controller->index($data,$view);
运行实例 »
点击 "运行实例" 按钮查看在线实例
<2>构造方法依赖注入
实例
<?php Class Controller{ protected $model; protected $view; //************************************************ //构造方法依赖注入 public function __construct(Model $model,View $view){ $this->model = $model; $this->view = $view; } //************************************************ //构造方法依赖注入 public function index(){ $rows = $this->model->getDate(1); $p = $this->model->getPage(); $pages =$this->model->getPages(); echo $this->view->viewHtml($rows,$p,$pages); } } //****************************************** //导入类 require "./../model/Model.php"; require "./../view/View.php"; //******************************************************* //构造方法中进行依赖注入 $model = new Model("staff",5); //var_dump($model); $view = new View(); $controller =NEW Controller($model,$view); echo $controller->index();
运行实例 »
点击 "运行实例" 按钮查看在线实例
效果图: