1. 依賴注入案例:
Model模型代碼:
<?php namespace Model; class Model{ public function getData(){ return [ ['name'=>'張志偉','sex'=>'男','age'=>22,'marriage'=>'未婚','wages'=>8000,'occupation'=>'外賣騎手'], ]; } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
View視圖代碼:
<?php namespace View; class View{ public function fetch($data){ foreach ($data as $val){ return '姓名:'.$val['name'].',性別:'.$val['sex'].',年齡:'.$val['age'].',婚姻狀態(tài):'.$val['marriage'].',職業(yè):'.$val['occupation'].',月薪:'.$val['wages']; } } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
Controller控制器代碼:
<?php namespace Controller; require 'Model.php'; use Model\Model; require 'View.php'; use View\View; $model = new Model(); $view = new View(); class Controller{ public function Index(Model $model,View $view){ $data = $model->getData(); return $view->fetch($data); } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
入口文件:demo1.php:
<?php require 'Controller.php'; use Controller\Controller; $controller = new Controller(); echo $controller->Index($model,$view);
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
效果瀏覽圖:
2、容器案例:
container 容器代碼:
<?php namespace Container; require 'Model.php'; use Model\Model; require 'View.php'; use View\View; class container { protected $rong = []; public function bind($key,$val){ $this->rong[$key]=$val; } public function make($key,$valss=[]) { return call_user_func_array($this->rong[$key],[]); } } $container = new container(); $container->bind('model',function (){ return new Model();}); $container->bind('view',function (){ return new View();});
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
Controller1 控制器代碼:
<?php namespace Controller1; require 'Container.php'; use Container\container; class Controller1{ public function Index($container){ $data = $container->make('model')->getData(); return $container->make('view')->fetch($data); } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
demo2.php 入口文件:
<?php require "Controller1.php"; use Controller1\Controller1; $controller1 = new Controller1(); echo $controller1->Index($container);
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
3、Facade 案例:
Facade.php類:
<?php namespace Facade; require 'Container.php'; use Container\container; class Facade { protected static $conrainer = null; protected static $data = []; public static function ini($conrainer){ static::$conrainer = $conrainer; } public static function getData(){ static::$data = static::$conrainer->make('model')->getData(); } public static function fecth(){ return static::$conrainer->make('view')->fetch(static::$data); } } // 繼承Facade class Student extends Facade{ }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
Controller2.php 控制器:
<?php namespace Controller2; require 'Facade.php'; use Facade\Student; class Controller2{ public function __construct($conrainer) { Student::ini($conrainer); } public function Index(){ Student::getData(); return Student::fecth(); } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
demo3.php 入口文件:
<?php require "Controller2.php"; use Controller2\Controller2; $controller = new Controller2($container); echo $controller->Index();
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
4、路由實(shí)現(xiàn)的基本原理:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)