登录  /  注册
博主信息
博文 38
粉丝 0
评论 0
访问量 29654
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
单例模式和MVC设计模式总结——2018年9月13日
图图的博客
原创
878人浏览过

单例模式:

实例

<?php
/**
 *  单例模式
 * 只允许实例一次
 * 连接数据库
 */
class Config{
    private static $demo= null;
//设置参数容器
    public $setting = [];
    //禁止从外部实例化对象
    private function __construct()
    {

    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
    //通过一个公共的静态方法创建对象

    public static function getInstance()
    {
        self::$demo = new self();
        return self::$demo;
    }
    //设置项的设置操作
    public function set()
    {
        //获取参数的数量
        $num = func_num_args();
        if ($num > 0){
            switch ($num) {
                case 1://只有一个参数说明是一个数组
                    $value = func_get_arg(0);
                    if (is_array($value)) {
                        //把参数加到$setting数组中
                        $this->setting = array_merge($this->setting, $value);
                    }
                    break;
                case 2://逐个设置
                    $name = func_get_arg(0);
                    $value = func_get_arg(1);
                    $this->setting[$name] = $value;
                    break;
                default:
                    echo '非法参数';
            }
        } else {
            echo '没有参数';
        }
    }


    //获取参数:当无参数时默认获取全部参数

    public function get($name = '')
    {
        if(empty($name)){
            return $this->setting;
        }
        return $this->setting[$name];
    }
}
$conf = Config::getInstance();
$conf->set('host','127.0.0.1');
echo $conf->get('host');
$config = ['host'=>'localhost','user'=>'root','pass'=>'123456'];
$conf->set($config);
print_r($conf->get());

运行实例 »

点击 "运行实例" 按钮查看在线实例

MVC设计模式:

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12
 * Time: 21:33
 */

namespace mvc\model;


class Model
{
    public $pdo;
    public $row;
    //连接数据库
    public $result = [];
    public function __construct($db,$user,$pass)
    {
        $this->pdo = new \PDO('mysql:host=127.0.0.1;dbname='.$db,$user,$pass);
    }

    public function select($num)
    {
        //创建预处理对象
        //准备SQL语句
        $sql = "SELECT * FROM `player`LIMIT :num ";


//创建预处理对象
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(':num',$num,\PDO::PARAM_INT);


        $stmt->execute();
        $this->row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}






//执行查询

//echo var_export($row),'<br>';

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12
 * Time: 21:33
 */

namespace mvc\model;


class Model
{
    public $pdo;
    public $row;
    //连接数据库
    public $result = [];
    public function __construct($db,$user,$pass)
    {
        $this->pdo = new \PDO('mysql:host=127.0.0.1;dbname='.$db,$user,$pass);
    }

    public function select($num)
    {
        //创建预处理对象
        //准备SQL语句
        $sql = "SELECT * FROM `player`LIMIT :num ";


//创建预处理对象
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(':num',$num,\PDO::PARAM_INT);


        $stmt->execute();
        $this->row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}






//执行查询

//echo var_export($row),'<br>';

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12
 * Time: 21:33
 */

namespace mvc\model;


class Model
{
    public $pdo;
    public $row;
    //连接数据库
    public $result = [];
    public function __construct($db,$user,$pass)
    {
        $this->pdo = new \PDO('mysql:host=127.0.0.1;dbname='.$db,$user,$pass);
    }

    public function select($num)
    {
        //创建预处理对象
        //准备SQL语句
        $sql = "SELECT * FROM `player`LIMIT :num ";


//创建预处理对象
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(':num',$num,\PDO::PARAM_INT);


        $stmt->execute();
        $this->row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
}






//执行查询

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/13
 * Time: 21:28
 */

namespace mvc\view;


class View
{
    public $data = [];
    //模板赋值
    public function __construct($data)
    {
        $this->data = $data;
    }

    //获取数据
    public function getData()
    {
        return $this->data;
    }

    //渲染
    public function display($data)
    {
        $table = '<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>MVC——球员信息表</title>
    <style>
    table,th,td{
    border: black solid 1px;
    }
    table{
    border-collapse: collapse;
    width: 40%;
    margin: 30px auto;
    text-align: center;
    }
    caption{
    font-size: 30px;
    font-weight: bold;
    margin-bottom:10px ;
    }
    table tr:first-child{
    background: #3a7ead;
    }
</style>
</head>
<body>
    <table>
        <caption>球员信息表</caption>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>年薪(万)</th>
        </tr>';

        foreach ($data as $player){
            $table .= '<tr>';
            $table .='<td>'.$player['id'].'</td>';
            $table .='<td>'.$player['name'].'</td>';
            $table .='<td>'.$player['age'].'</td>';
            $table .='<td>'.$player['salary'].'</td>';
            $table .= '</tr>';
        }
        $table .='</table></body></html>';

        echo $table;

    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/13
 * Time: 21:52
 */

namespace mvc\controller;
use mvc\model\Model;
use mvc\view\View;
class Controller
{
    public function index()
    {
        require './model/Model.php';

        $model = new Model('php','root','root');
        $model->select(10);
        $res = $model->row;
//print_r($res);


        require './view/View.php';

        $view = new View($res);

        $data = $view->getData();
        $view->display($data);
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/12
 * Time: 21:42
 */
require './controller/Controller.php';
use \mvc\controller\Controller;
$controller = new Controller;
$controller->index();

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学