单例模式:
实例
<?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();
运行实例 »
点击 "运行实例" 按钮查看在线实例