Blogger Information
Blog 48
fans 0
comment 0
visits 36359
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单例模式、工厂模式、mvc模式-2017.09.07
雨天的博客
Original
718 people have browsed it

/**
* 单例模式
* 1.类只允许被实例化一次;
* 2.类中只提供私有构造函数;
* 3.类中含有一个静态私有的对象;
* 4.类中提供一个公有静态方法供类,用于类创建本身的静态私有对象;
*/

实例

<?php
class config{
    //创建私有对象
    private static $instance = null;
    //参数容器
    private $config;

    public  $setting = [];
    //私有构造方法 禁止从类的外部实例化类
    private function __construct()
    {
    }
    //禁止克隆类
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
    //公有静态方法
    public static function getInstance()
    {
        if(self::$instance == null)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
    public  function  set()
    {
        $num = func_num_args();//返回函数参数的个数
        if($num>0)
        {
            switch ($num)
            {
                case 1:
                    $value = func_get_arg(0);
                    if(is_array($value))
                    {
                        $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;
            }


        }else{
            echo '<span>没有传入参数</span>';
        }
    }
    function  get($name='')
    {
        if(empty($name))
        {
            return $this->setting;
        }
        return $this->setting[$name];
    }
}
$cfg = config::getInstance();
//$cfg -> set('host','localhost');
//echo $cfg->get('host');
$config = [
    'host'=>'localhost',
    'user'=>'root',
    'pwd'=>'root'
];
$cfg -> set($config);
echo '<pre>';
var_dump($cfg->get());

运行实例 »

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

mvc模式

//模型层 Model

实例

<?php
//模型类

namespace mvc\model;


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

    //查询
    public function select($table, $num)
    {
        //创建预处理对象
        $stmt = $this->pdo->prepare("SELECT `id`,`name`,`age`,`salary` FROM {$table} LIMIT :num");
        //执行查询
        $stmt->bindValue(':num', $num, \PDO::PARAM_INT);
        $stmt->execute();

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

运行实例 »

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

//视图层View

实例

<?php
/*
 * 视图类
 */


namespace mvc\view;


class View
{
    public $data = [];
    function __construct($data)
    {
        $this->data = $data;
    }
    function  getdata()
    {
        return $this->data;
    }
        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>Document</title>
</head>
<body>';

        $table.='<table border="1" cellspacing="0" cellpadding="0" width="400" align="center"><tr style="background: #eb7350;color: #FFF"><th>ID</th><th>姓名</th><th>地址</th></tr>';

        foreach ($data as $stu)
        {
            $table .='<tr align="center"><td>'.$stu['id'].'</td><td>'.$stu['name'].'</td><td>'.$stu['address'].'</td></tr>';
        }

            $table.='</table>
</body>
</html>';
        echo $table;
    }
}

运行实例 »

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

//控制层 Controller

实例

<?php
/**
 * 控制层
 */
namespace mvc\controller;

use mvc\model\Model;
use mvc\view\View;
class Controller
{

    function index()
    {

        require './mvc/model/Model.php';
        $model = new Model('mysql:host=localhost;dbname=data','root','root');
        $result  = $model->select('stu',5);

        require'./mvc/view/View.php';
        $view = new View($result);
        $data = $view->getdata();
        $view->display($data);

    }
}

运行实例 »

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

//框架入口文件

实例

<?php
/**
 * 框架入口文件
 */
require './mvc/controller/Controller.php';
use mvc\controller\Controller;
$index= new Controller();
$index->index();

运行实例 »

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

* mvc模式的设计思想:
* M:model封装了数据库的链接和数据的处理操作;
* V:view封装了对数据model层的展示效果;
* C:contrller model层和view层的调度者;

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!