批改状态:合格
老师批语:
这一节课主要讲的是类和数据库的知识,主要是类和对象,类的构造器,魔术方法__get()和__set(),mysql连接数据库1、
什么是类 ?什么是对象?举例说明
答:类是对象的模板,对象是类的一个实例。 举例:类:class Student{} 对象:$student=new Student()
代码
<?php
class Student{//定义一个Student类
//访问控制:私有化pricate
private $name;
//年龄
private $age;
//成绩分数
private $score=[];
//属性收集器
private $data=[];
//声明构造方法:对象属性的初始化,在类实例化时自动调用
public function __construct($name,$age,array $score)
{//private访问符限制的属性仅在当前对象内部可以使用
$this->name = $name;
$this->age = $age;
$this->score = $score;
}
//创建对外访问的公共接口
//类中用双下划线的方法是系统定义的,由系统自动调用是魔术方法
public function __get($name)
{
$msg = null;
if(isset($this->name)){
$msg = $this->$name;
}elseif (isset($this->data[$name])){
$msg = $this->data[$name];
}else{
$msg = '无此属性';
}
return $msg;
}
//设置器
public function __set($name, $value)
{
$this->name = $value;
}
}
?>点击 "运行实例" 按钮查看在线实例
<?php
//插入外部的类文件
require 'class/Student.php';
//类的实例化
$student1 = new Student('杨过',26,[80,90,86]);
echo $student1->name,'<br>';
echo $student1->age,'<br>';
print_r($student1->score) ;点击 "运行实例" 按钮查看在线实例
预览图

代码
//在表staff中插入数据 insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); insert into `staff` (`name`,`salary`) values (`aaa`,4544); //更新staff表中id=17的age和salary字段 update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; update `staff` set `age`=30,`salary`=1000 where `id`=17; //查询staff表 select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; select * from `staff` where 条件; //删除 delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件; delete * from `staff` where 条件;
点击 "运行实例" 按钮查看在线实例
代码
<?php $db = [ 'host' => '127.0.0.1', 'user' => 'root', 'pass' => '5201314', 'name' => 'php', 'charset' => 'utf8', ];
点击 "运行实例" 按钮查看在线实例
<?php
//插入配置文件
require 'config.php';
//创建一个数据库连接,并返回mysqli对象
$mysqli= new mysqli($db['host'],$db['user'],$db['pass'], $db['name']);
//判断是否连接成功
if($mysqli->connect_errno){
//自定义错误提示信息
die('连接错误'.$mysqli->connect_errno.':'.$mysqli->connect_error);
}
echo '<h1>连接成功</h1>';
//设置客 户端默认的字符编码集
$mysqli->set_charset($db['charset']);点击 "运行实例" 按钮查看在线实例
预览图

手写
![1535601081555778.png P53P(6]EXPT)5($~076ZYVW.png](https://img.php.cn//upload/image/250/438/259/1535601081555778.png)
对于数据库连接用到的属性和方法记得更加清楚,也知道怎样去写数据库连接
总结
1、知道什么是类什么是对象,类是对象的模板,对象是类的一个实例
2、怎么写公共接口__set()和get(),同时知道了怎么进行访问
3、熟悉了MySQL常用的增删改查语句
4、msql的连接所用到的属性和方法以及adminer.php的使用
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号