1.MVC实例
建库建表sql:
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP DATABASE IF EXISTS `edu`;
CREATE DATABASE IF NOT EXISTS `edu` DEFAULT CHARSET = `utf8`;
USE `edu`;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(4) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`name` varchar(50) NOT NULL COMMENT '姓名',
`email` varchar(100) NOT NULL COMMENT '邮箱',
`course` varchar(100) NOT NULL COMMENT '课程',
`grade` int(4) NOT NULL COMMENT '成绩',
`create_time` int(10) NOT NULL COMMENT '创建时间',
`update_time` int(10) NOT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生信息表';
INSERT INTO `student` (`id`,`name`,`email`,`course`,`grade`,`create_time`,`update_time`)
VALUES
(1,'郭靖','guojing@php.cn','php',80,1505054471,1505054471),
(2,'黄蓉','huangrong@php.cn','mysql',88,1505054471,1505054471),
(3,'杨康','yangkang@php.cn','mysql',67,1505054471,1505054471),
(4,'洪七','hongqi@php.cn','php',35,1505054471,1505054471),
(5,'老顽童','laowantong@php.cn','html',78,1505054471,1505054471),
(6,'欧阳峰','ouyangfeng@php.cn','mysql',56,1505054471,1505054471),
(7,'杨过','yangguo@php.cn','php',89,1505054471,1505054471),
(8,'小龙女','xiaolongnv@php.cn','html',800,1505054471,1505054471),
(9,'张无忌','zhangwuji@php.cn','mysql',63,1505054471,1505054471),
(10,'赵敏', 'zhaomin@php.cn','php', 80,1505054471,1505722385);
model.php:
<?php
/**
* 获取数据 Model 模型层
*/
$conn = mysqli_connect('localhost', 'root', '123456', 'edu');
$result = mysqli_query($conn, "SELECT * FROM student");
if ($result && mysqli_num_rows($result) > 0) {
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC); // $rows是二维数组
} else {
echo '<h2>没有数据返回</h2>';
}
view.php:
<?php
/**
* 显示数据 View 视图层
*/
echo '<h2 align="center">学生信息表</h2>';
echo '<table border="1" cellspacing="0" cellpadding="5" align="center" width="60%">';
echo '<tr align="center" bgcolor="#87ceeb"><th>id</th><th>姓名</th><th>邮箱</th><th>课程</th><th>成绩</th></tr>';
foreach ($rows as $row) {
echo '<tr align="center">';
echo '<td>'.$row['id'].'</td><td>'.$row['name'].'</td><td>'.$row['email'].'</td><td>'.$row['course'].'</td><td>'.$row['grade'].'</td>';
echo '</tr>';
}
echo '</table>';
mvc.php:
<?php
/**
* mvc编程思想
* 控制器:Controller
* 获取并显示,实现数据与视图之间的整合
*/
// 获取数据 Model 模型层
include_once 'model.php';
// 显示数据 View 视图层
include_once 'view.php';
执行效果:
2.单例模式
<?php
//单例模式:确保仅生成当前类的唯一实例
/**
* 单例模式
*/
class Demo
{
// 1.静态私有属性,保存当前类的实例
private static $instance = null;
// 2.构造方法私有化,禁止外部用new来创建类实例
private function __construct()
{
}
// 3.克隆方法私有化,禁止从外部克隆来生成本类的实例
private function __clone()
{
// TODO: Implement __clone() method.
}
// 4.生产当前类的唯一实例
// 因为这个方法要使用静态属性,所以必须声明为静态
public static function getInstance()
{
// 如果不是当前类的实例,那么实例化当前类创建新实例
if (!self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
}
//$obj1 = new Demo();
//$obj2 = new Demo();
//$obj3 = new Demo();
//$obj4 = new Demo();
// 对象间的复制
//$obj5 = $obj4; // 引用赋值
//$obj6 = clone $obj4;
//var_dump($obj1, $obj2, $obj3, $obj4, $obj5);
$obj1 = Demo::getInstance();
$obj2 = Demo::getInstance();
$obj3 = Demo::getInstance();
$obj4 = Demo::getInstance();
$obj5 = Demo::getInstance();
var_dump($obj1, $obj2, $obj3, $obj4, $obj5);
执行效果:
object(Demo)#1 (0) { } object(Demo)#1 (0) { } object(Demo)#1 (0) { } object(Demo)#1 (0) { } object(Demo)#1 (0) { }